ElasticSearch:在对象数组中搜索

Nik*_*ita 11 search-engine elasticsearch

我在查询数组中的对象时遇到问题.让我们创建一个非常简单的索引,添加一个带有一个字段的类型,并添加一个带有对象数组的文档(我使用感应控制台):

PUT /test/
PUT /test/test/_mapping
{
    "test": {
        "properties": {
            "parent": {"type": "object"}
        }
    }
}
Post /test/test
 {
     "parent": [
                  {
                     "name": "turkey",
                     "label": "Turkey"
                  },
                  {
                     "name": "turkey,mugla-province",
                     "label": "Mugla (province)"
                  }
               ]
 }   
Run Code Online (Sandbox Code Playgroud)

现在我想通过这两个名称进行搜索"turkey""turkey,mugla-province".第一个查询工作正常:

GET /test/test/_search {"query":{ "term": {"parent.name": "turkey"}}}

但第二个没有回报:

GET /test/test/_search {"query":{ "term": {"parent.name": "turkey,mugla-province"}}}

我尝试了很多东西,包括:

 "parent": {
              "type": "nested",
              "include_in_parent": true,
              "properties": {
                 "label": {
                    "type": "string",
                    "index": "not_analyzed"
                 },
                 "name": {
                    "type": "string",
                    "store": true
                 }
              }
           }
Run Code Online (Sandbox Code Playgroud)

但没有任何帮助.我错过了什么?

Slo*_*ens 11

这是使用嵌套文档的一种方法:

我定义了一个这样的索引:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "parent": {
               "type": "nested",
               "properties": {
                  "label": {
                     "type": "string"
                  },
                  "name": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

索引您的文档:

PUT /test_index/doc/1
{
   "parent": [
      {
         "name": "turkey",
         "label": "Turkey"
      },
      {
         "name": "turkey,mugla-province",
         "label": "Mugla (province)"
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

然后这些查询之一将返回它:

POST /test_index/_search
{
    "query": {
        "nested": {
           "path": "parent",
           "query": {
               "match": {
                  "parent.name": "turkey"
               }
           }
        }
    }
}

POST /test_index/_search
{
    "query": {
        "nested": {
           "path": "parent",
           "query": {
               "match": {
                  "parent.name": "turkey,mugla-province"
               }
           }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码:

http://sense.qbox.io/gist/6258f8c9ee64878a1835b3e9ea2b54e5cf6b1d9e


Dan*_*ing 6

对于搜索多个术语,请使用术语查询而不是术语查询.

"terms" : {
        "tags" : [ "turkey", "mugla-province" ],
        "minimum_should_match" : 1
    }
Run Code Online (Sandbox Code Playgroud)

构建此查询有多种方法,但这是当前版本的ElasticSearch(1.6)中最简单和最优雅的方法