ElasticSearch: search inside the array of objects

旧街凉风 提交于 2020-07-04 07:52:09

问题


I have a problem with querying objects in array. Let's create very simple index, add a type with one field and add one document with array of objects (I use sense console):

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

Now I want to search by both names "turkey" and "turkey,mugla-province" . The first query works fine:

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

But the second one returns nothing:

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

I tried a lot of stuff including:

"parent": {
    "type": "nested",
    "include_in_parent": true,
    "properties": {
         "label": {
             "type": "string",
             "index": "not_analyzed"
         },
         "name": {
             "type": "string",
             "store": true
         }
     }
}

But nothing helps. What do I miss?


回答1:


Here's one way you can do it, using nested docs:

I defined an index like this:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "parent": {
               "type": "nested",
               "properties": {
                  "label": {
                     "type": "string"
                  },
                  "name": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}

Indexed your document:

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

Then either of these queries will return it:

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

Here's the code I used:

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




回答2:


For search multiple terms use the Terms query instead of Term query.

"terms" : {
        "tags" : [ "turkey", "mugla-province" ],
        "minimum_should_match" : 1
    }

There are various ways to construct this query, but this is the simplest and most elegant in the current version of ElasticSearch (1.6)



来源:https://stackoverflow.com/questions/31062264/elasticsearch-search-inside-the-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!