ElasticSearch:在对象数组内搜索 [英] ElasticSearch: search inside the array of objects
问题描述
查询数组中的对象时遇到问题。
让我们创建一个非常简单的索引,添加一个具有一个字段的类型并添加一个包含对象数组的文档(我使用感知控制台):
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?
推荐答案
这是使用嵌套文档:
我定义了这样的索引:
PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"parent": {
"type": "nested",
"properties": {
"label": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
}
为您的文档建立索引:
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"
}
}
}
}
}
这是我使用的代码:
http://sense.qbox.io/gist/6258f8c9ee64878a1835b3e9ea2b54e5cf6b1d9e
这篇关于ElasticSearch:在对象数组内搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!