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

时间:2015-03-03 11:28:11

标签: elasticsearch

我在elasticsearch中编制了索引数据。索引名称为" demo" 。 我有两种类型(映射)用于"演示" ,一个是"用户"和其他是#34;博客"。 "用户"类型有字段 - 名称,城市,国家其他字段和博客有 - "标题" ,描述" ," author_name"等等 现在我想搜索" demo"。如果我想搜索" java"那么它将带来所有文件,包括" java"在任何类型的任何领域,"用户"或者"博客"。

1 个答案:

答案 0 :(得分:6)

您可以将"_all" field用于该索引。默认情况下,您的每个字段都会包含在每种类型的"_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"
            }
         }
      ]
   }
}