首页 > ELK > elasticSerch同时查询多个索引
2019
09-08

elasticSerch同时查询多个索引

背景

许多场景需要查询的结果不在一个索引里面,但是elasticseach不能像别的数据库一样进行联查,所以我们可以同时查询多个索引的字段来满足需求

查询DSL语句

GET /_search
{
  "size": 20,
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "_index": {
                    "value": "索引一"
                  }
                }
              },
              {
                "term": {
                  "索引一字段": {
                    "value": "少年歌行"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "_index": {
                    "value": "索引二"
                  }
                }
              }
            ],
            "filter": {
              "term": {
                "索引而字段": "少年歌行"
              }
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_index": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

实战

GET /_search
 {
        "sort": {
            "_score": {
                "order": "desc"
            },
            "creat_time": {
                "order": "desc",
                        "unmapped_type":"long"
            }
        },
        "query": {
            "bool": {
                "minimum_should_match": 1,
                "should": [{
                    "bool": {
                        "must": [{
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "search_content": {
                                            "query": "资本市场",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "term": {
                                        "state": ""
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "term": {
                                        "content_charge": "1"
                                    }
                                }
                            }
                        }, {
                            "term": {
                                "_index": {
                                    "value": "es_msg_alias"
                                }
                            }
                        }],
                        "must_not": [{
                            "term": {
                                "kind": "vip"
                            }
                        }, {
                            "term": {
                                "view_self": "yes"
                            }
                        }, {
                            "term": {
                                "only_self_show": "yes"
                            }
                        }, {
                            "terms": {
                                "u_id": ["417961"]
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "search_content": {
                                            "query": "潜伏高中手",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "search_content": {
                                            "query": "测试账号test",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "search_content": {
                                            "query": "人民日报",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }],
                        "should": [{
                            "constant_score": {
                                "boost": 2,
                                "filter": {
                                    "match_phrase": {
                                        "title": {
                                            "query": "资本市场",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }]
                    }
                }, {
                    "bool": {
                        "must": [{
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "content": {
                                            "query": "资本市场",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "term": {
                                        "state": ""
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "term": {
                                        "l_id": "0"
                                    }
                                }
                            }
                        }, {
                            "term": {
                                "_index": {
                                    "value": "news_alias"
                                }
                            }
                        }],
                        "must_not": [{
                            "term": {
                                "kind": "vip"
                            }
                        }, {
                            "term": {
                                "view_self": "yes"
                            }
                        }, {
                            "term": {
                                "only_self_show": "yes"
                            }
                        }, {
                            "terms": {
                                "u_id": ["417961"]
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "content": {
                                            "query": "潜伏高中手",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "content": {
                                            "query": "测试账号test",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }, {
                            "constant_score": {
                                "boost": 0,
                                "filter": {
                                    "match_phrase": {
                                        "content": {
                                            "query": "人民日报",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }],
                        "should": [{
                            "constant_score": {
                                "boost": 2,
                                "filter": {
                                    "match_phrase": {
                                        "title": {
                                            "query": "资本市场",
                                            "slop": 0
                                        }
                                    }
                                }
                            }
                        }]
                    }
                }]
            }
        },
        "from": 0,
        "size": "10"
    }


本文》有 0 条评论

留下一个回复