Elasticsearch - 搜索多种类型的索引及其不同类型

Abh*_*nyu 1 elasticsearch

我在elasticsearch中索引了数据.索引名称是"demo".我有两种类型(映射)用于"演示",一种是"用户",另一种是"博客"."用户"类型有字段 - 名称,城市,国家其他字段和博客 - "标题",描述","author_name"等现在我想搜索"演示".如果我想搜索"java"然后它将带有"java"的所有文档带入任何类型的任何字段,"user"或"blog".

Slo*_*ens 6

您可以使用该索引的"_all"字段.默认情况下,您的每个字段都将包含在"_all"每种类型的字段中.然后,您可以match对该"_all"字段运行查询.此外,在搜索索引时,只需不指定类型即可搜索所有类型.

这是一个例子:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "user": {
         "properties": {
             "name" : { "type": "string" },
             "city" : { "type": "string" },
             "country" : { "type": "string" }
         }
      },
      "blog": {
         "properties": {
             "title" : { "type": "string" },
             "description" : { "type": "string" },
             "author_name" : { "type": "string" }
         }
      }
   }
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"user"}}
{"name":"Bob","city":"New York","country":"USA"}
{"index":{"_index":"test_index","_type":"user"}}
{"name":"John","city":"Jakarta","country":"Java/Indonesia"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Python/ES","description":"using Python with Elasticsearch","author_name":"John"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Java/ES","description":"using Java with Elasticsearch","author_name":"Bob"}

POST /test_index/_search
{
    "query": {
        "match": {
           "_all": "Java"
        }
    }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.68289655,
      "hits": [
         {
            "_index": "test_index",
            "_type": "blog",
            "_id": "hNJ-AOG2SbS0nw4IPBuXGQ",
            "_score": 0.68289655,
            "_source": {
               "title": "Java/ES",
               "description": "using Java with Elasticsearch",
               "author_name": "Bob"
            }
         },
         {
            "_index": "test_index",
            "_type": "user",
            "_id": "VqfowNx8TTG69buY9Vd_MQ",
            "_score": 0.643841,
            "_source": {
               "name": "John",
               "city": "Jakarta",
               "country": "Java/Indonesia"
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)