2012-11-30 42 views
3

我正在试验ElasticSearch。我在查询嵌套对象时遇到问题。嵌套的查询不按预期工作

我的映射:

curl -X GET http://localhost:9200/testt/resource/_mapping?pretty

{ 
    "resource": { 
     "properties": { 
      "bib": { 
       "type": "nested", 
       "properties": { 
        "IssueDate": { 
         "type": "date", 
         "format": "dateOptionalTime" 
        }, 
        "Title": { 
         "type": "string" 
        } 
       } 
      }, 
      "name": { 
       "type": "string" 
      } 
     } 
    } 
} 

我有一个索引资源:

curl -X GET http://localhost:9200/testt/resource/_search?pretty

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1.0, 
     "hits": [ 
      { 
       "_index": "testt", 
       "_type": "resource", 
       "_id": "1234", 
       "_score": 1.0, 
       "_source": { 
        "name": "SSS", 
        "bib": { 
         "Title": "XSD", 
         "IssueDate": "2012-12-19" 
        } 
       } 
      } 
     ] 
    } 
} 

curl -X GET http://localhost:9200/testt/resource/1234?pretty

{ 
    "_index": "testt", 
    "_type": "resource", 
    "_id": "1234", 
    "_version": 1, 
    "exists": true, 
    "_source": { 
     "name": "SSS", 
     "bib": { 
      "Title": "XSD", 
      "IssueDate": "2012-12-19" 
     } 
    } 
} 

然而,我可以使用查询请求无法找到它:

{ 
    "query": { 
     "nested": { 
      "path": "bib", 
      "query": { 
       "query_string": { 
        "query": "XSD" 
       } 
      } 
     } 
    } 
} 

搜索:curl -X GET http://localhost:9200/testt/resource/_search?pretty -d '{ "query" : { "nested" : {"path" : "bib", "query" : { "query_string" : {"query" : "XSD"} } } } }'

{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
     "total" : 5, 
     "successful" : 5, 
     "failed" : 0 
    }, 
    "hits" : { 
     "total" : 0, 
     "max_score" : null, 
     "hits" : [ ] 
    } 
} 

我的问题是:我该如何使用嵌套查询找到我的对象?我对嵌套对象bib的对象感兴趣,它包含字XSD。对象1234明确包含XSD,但我无法找到它。你能告诉我我的查询是否可以吗?它出什么问题了?

回答

3

query_string不支持它,但如果你能自己分析查询,你可以使用multi_match查询,做这样的事情:

{ 
    "query": { 
     "nested": { 
      "path": "bib", 
      "query": { 
       "multi_match": { 
        "query": "XSD", 
        "fields": ["bib.*"] 
       } 
      } 
     } 
    } 
} 

一个可能的问题与此解决方案是,如果你有嵌套的文档中的任何数字字段,你需要从字段列表中排除。它可以通过向字段名称添加前缀来实现。例如,您可以重命名所有字符串字段以s_开头,在这种情况下,您可以使用"fields": ["bib.s_*"]选择所有字符串字段。

另一种可能的解决方案是使用父母的_all字段。您可以排除_all中的所有父级字段,并专门为嵌套字段使用_all。所有嵌套字段默认都包含在父项的_all字段中。

+0

我喜欢你的答案。我如何实现'_all'的排除?你可以提供示例查询与排除? –

+0

它不在查询中。它在映射中。你需要指定''include_in_all“:false'。有关更多信息,请参阅http://www.elasticsearch.org/guide/reference/mapping/all-field.html。 – imotov

+0

我可能会使用“s_”前缀。谢谢你的帮助。 –

2

你需要在你的query_string query指定默认领域:

curl -XGET localhost:9200/testt/resource/_search -d '{ 
    "query": { 
     "nested" : { 
      "path" : "bib", 
      "score_mode" : "avg", 
      "query" : { 
       "query_string" : { 
        "fields" : ["Title"], 
        "query" : "XSD" 
       } 
      } 
     } 
    } 
}'; 
+0

这不是我的问题的答案。我想扫描所有字段。关于文档中的'default_field':'默认为index.query.default_field索引设置,默认设置为_all.'它是ElasticSearch中的缺陷吗?我认为是这样,因为我的查询不像文档所说的那样工作。 –

+0

您必须明确设置所有字段,然后:''query_string“:{”{“title”,“Subtitle”] { “query”:“XSD” }'[Nested documents](http: /www.elasticsearch.org/guide/reference/mapping/nested-type.html)没有自己的'_all'字段,它们使用父文档。 – Thorsten

+0

这是正确的,所以如果你将搜索父对象的_all,你将能够找到你的文档'curl -XGET localhost:9200/testt/resource/_search -d'{“query”:{“query_string”:{ “query”:“XSD”}}}'' – imotov