我在查询数组中的对象时遇到问题。让我们创建一个非常简单的索引,使用一个字段添加一个类型,并使用对象数组添加一个文档(我使用感知控制台):
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)"
}
]
}
现在,我要同时搜索名称"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
}
}
}
但是没有帮助。我想念什么?
这是使用nested文档的一种方法:
我定义了这样的索引:
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)"
}
]
}
然后这些查询中的任何一个都将返回它:
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
要搜索多个术语,请使用术语查询而不是术语查询。
"terms" : {
"tags" : [ "turkey", "mugla-province" ],
"minimum_should_match" : 1
}
构造此查询的方法多种多样,但这是当前版本的ElasticSearch(1.6)中最简单,最优雅的方法>>