首页 > 解决方案 > 弹性搜索 - 带有字段映射的多个索引

问题描述

我需要搜索多个索引及其字段映射。

例如我想查询一个字符串,在

field1 with index1

field2 with index2
from elasticsearch import Elasticsearch
es = Elasticsearch([eshost])
req_string = {
  "size":1000,
  "query": {
    "query_string": {
      "query": "string to be searched",
      "fields": ["field1","field2"],
    }
  }
}

res = es.search(index='index1,index2', body=req_string)

有可能吗?如果是,请提供一些链接。提前致谢 !

标签: pythonelasticsearch

解决方案


跨多个索引查询时,您可以使用_index 字段。

_index 字段允许匹配文档被索引到的索引。它的值可在某些查询和聚合中以及在排序或编写脚本时访问

添加带有索引数据、搜索查询和搜索结果的工作示例。

指数数据:

PUT/ index1/_doc/1
    {
        "name": "Hello"
    }

PUT/ index2/_doc/1
{
    "name": "Hello World"
}

搜索查询:

{
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "_index": [
                            "index1",
                            "index2"
                        ]
                    }
                }
            ],
            "must": [
                {
                    "simple_query_string": {
                        "query": "hello",
                        "fields": [
                            "name",
                            "title"
                        ]
                    }
                }
            ]
        }
    }
}

搜索结果:

"hits": [
  {
    "_index": "index2",
    "_type": "_doc",
    "_id": "1",
    "_score": 0.4700036,
    "_source": {
      "name": "Hello World"
    }
  },
  {
    "_index": "index1",
    "_type": "_doc",
    "_id": "1",
    "_score": 0.2876821,
    "_source": {
      "title": "Hello"
    }
  }
]

更新的搜索查询:

下面的搜索查询将搜索仅在中的title字段index1name仅在中的字段index2

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "filter": [
                            {
                                "terms": {
                                    "_index": [
                                        "index1"
                                    ]
                                }
                            }
                        ],
                        "must": [
                            {
                                "query_string": {
                                    "query": "hello",
                                    "fields": [
                                        "title"
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "filter": [
                            {
                                "terms": {
                                    "_index": [
                                        "index2"
                                    ]
                                }
                            }
                        ],
                        "must": [
                            {
                                "query_string": {
                                    "query": "hello",
                                    "fields": [
                                        "name"
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

推荐阅读