我正在尝试用一个简单的示例应用程序学习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,并返回匹配的人。例如,我可能想要找到一些人,他们的引文中包含(this AND This)--应该返回"Mr A“而不是"Mr B",等等。我如何才能做到这一点?
EDIT1:
安德烈下面的答案似乎是有效的,现在的数据值如下所示:
{"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:是的,请看安德烈的回答。
发布于 2014-10-08 23:04:44
为了实现这一要求,您需要查看嵌套对象,而不是查询一个扁平化的值列表,而是查询该嵌套对象中的单个值。例如:
{
"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"}}
]
}
}
}
}
}发布于 2014-10-08 22:17:11
不幸的是,没有好的方法来做到这一点。https://web.archive.org/web/20141021073225/http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/complex-core-fields.html
当您从Elasticsearch返回一个文档时,任何数组的顺序都将与您为文档编制索引时的顺序相同。返回的JSON域包含与索引的_source文档完全相同的内容。
但是,数组被编入索引,使得 - 可作为无序的多值字段进行搜索。在搜索时,您不能引用“第一个元素”或“最后一个元素”。更确切地说,可以将数组看作是值的袋子。
换句话说,它总是考虑数组中的所有值。
这将只返回先生A
{
"query": {
"match": {
"quotations": {
"query": "quotation one",
"operator": "AND"
}
}
}
}但这将同时返回A先生和B先生:
{
"query": {
"match": {
"quotations": {
"query": "this these",
"operator": "AND"
}
}
}
}发布于 2018-09-17 09:23:28
如果启用了scripting,这应该会起作用:
"script": {
"inline": "for(element in _source.quotations) { if(element == 'this' && element == 'these') {return true;} }; return false;"
}https://stackoverflow.com/questions/26258292
复制相似问题