我正在尝试通过一个简单的示例应用程序学习Elasticsearch,该应用程序列出了与人相关的报价。映射示例如下所示:
{ "people" : { "properties" : { "name" : { "type" : "string"}, "quotations" : { "type" : "string" } } } }
一些示例数据可能看起来像:
{ "name" : "Mr A", "quotations" : [ "quotation one, this and that and these" , "quotation two, those and that"] } { "name" : "Mr B", "quotations" : [ "quotation three, this and that" , "quotation four, those and these"] }
我希望能够对单个引用使用querystring api,并返回匹配的人。例如,我可能想查找报价包含(此与这些)的人-应该返回“ A先生”而不是“ B先生”,依此类推。我该如何实现?
编辑1:
安德烈(Andrei)在下面的回答似乎可行,数据值现在看起来像:
{"name":"Mr A","quotations":[{"value" : "quotation one, this and that and these"}, {"value" : "quotation two, those and that"}]}
但是,我似乎无法使query_string查询正常工作。以下没有结果:
{ "query": { "nested": { "path": "quotations", "query": { "query_string": { "default_field": "quotations", "query": "quotations.value:this AND these" } } } } }
有没有办法让query_string查询与嵌套对象一起工作?
Edit2:是的,请参阅Andrei的答案。
为了达到该要求,您需要查看嵌套对象,而不是查询平整的值列表,而是查询该嵌套对象中的各个值。例如:
{ "mappings": { "people": { "properties": { "name": { "type": "string" }, "quotations": { "type": "nested", "properties": { "value": { "type": "string" } } } } } } }
值:
{"name":"Mr A","quotations":[{"value": "quotation one, this and that and these"}, {"value": "quotation two, those and that"}]} {"name":"Mr B","quotations":[{"value": "quotation three, this and that"}, {"value": "quotation four, those and these"}]}
查询:
{ "query": { "nested": { "path": "quotations", "query": { "bool": { "must": [ { "match": {"quotations.value": "this"}}, { "match": {"quotations.value": "these"}} ] } } } } }