【问题标题】:Elasticsearch - searching across multiple multiple types of a index and its different typesElasticsearch - 搜索多种类型的索引及其不同类型
【发布时间】:2015-03-03 11:28:10
【问题描述】:

我在 elasticsearch 中索引了数据。索引名称是 "demo" 。 我有两种“演示”类型(映射),一种是“用户”,另一种是“博客”。 “用户”类型有字段 - name , city , country 其他字段和博客有 - “title” , description” , “author_name” 等。 现在我想搜索“演示”。如果我想搜索“java”,那么它将在任何类型的任何字段中包含“java”的所有文档,无论是“user”还是“blog”。

【问题讨论】:

  • 你试过什么解决方案?

标签: elasticsearch


【解决方案1】:

您可以将"_all" field 用于该索引。默认情况下,您的每个字段都将包含在每种类型的 "_all" 字段中。然后您可以针对"_all" 字段运行match 查询。另外,在搜索索引时,只要不指定类型,所有类型都会被搜索。

这是一个例子:

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"
            }
         }
      ]
   }
}

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多