查询字符串搜索弹性搜索中的数组元素 [英] Querystring search on array elements in Elastic Search
问题描述
我试图用一个简单的示例应用程序来学习弹性搜索,其中列出了与人相关的报价。示例映射可能如下所示:
I'm trying to learn elasticsearch with a simple example application, that lists quotations associated with people. The example mapping might look like:
{
"people" : {
"properties" : {
"name" : { "type" : "string"},
"quotations" : { "type" : "string" }
}
}
}
一些示例数据可能如下所示:
Some example data might look like:
{ "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"]
}
我想能够使用个人报价的查询字符串,并返回匹配的人。例如,我可能想找到那些包含(这个AND这些)的报价的人,这些人应该返回A先生,而不是B先生等等。如何实现这一点?
I would like to be able to use the querystring api on individual quotations, and return the people who match. For instance, I might want to find people who have a quotation that contains (this AND these) - which should return "Mr A" but not "Mr B", and so on. How can I achieve this?
EDIT1:
Andrei的下面的回答似乎正常,数据值现在正在如:
Andrei's answer below seems to work, with data values now looking like:
{"name":"Mr A","quotations":[{"value" : "quotation one, this and that and these"}, {"value" : "quotation two, those and that"}]}
但是,我似乎无法让query_string查询工作。以下不会产生结果:
However, I can't seem to get a query_string query to work. The following produces no results:
{
"query": {
"nested": {
"path": "quotations",
"query": {
"query_string": {
"default_field": "quotations",
"query": "quotations.value:this AND these"
}
}
}
}
}
有没有办法让一个query_string查询使用嵌套对象?
Is there a way to get a query_string query working with a nested object?
Edit2:是的,看到Andrei的
Yes it is, see Andrei's answer.
推荐答案
要实现这一要求,您需要查看嵌套对象,而不是查询平坦化的值列表但是来自该嵌套对象的个体值。例如:
For that requirement to be achieved, you need to look at nested objects, not to query a flattened list of values but individual values from that nested object. For example:
{
"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"}}
]
}
}
}
}
}
这篇关于查询字符串搜索弹性搜索中的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!