弹性搜索阵列元素的查询字符串搜索
我试图通过一个简单的示例应用程序来学习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"]
}
我想能够使用个人报价查询字符串API,并返回匹配谁的人。例如,我可能想要找到包含(这个和这些)引用的人 - 应该返回“A先生”而不是“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查询处理嵌套对象?
编辑2:是的,请参阅安德烈的答案。
对于要求来实现的,你需要看看嵌套的对象,不要查询展开的值列表,而是查询来自该嵌套对象的单个值。例如:
{
"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"}}
]
}
}
}
}
}
不幸的是没有好的方法来做到这一点。 http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/complex-core-fields.html
当你从一个文档Elasticsearch回来,任何阵列将在 相同的顺序,当您索引文件。您找回的_source字段 包含与您索引的 完全相同的JSON文档。
但是,数组被索引 - 可搜索 - 作为无值的多值字段 。在搜索时您不能引用“第一个 元素”或“最后一个元素”。宁可将数组视为一包 值。
换句话说,它总是考虑数组中的所有值。
这将返回只有A先生
{
"query": {
"match": {
"quotations": {
"query": "quotation one",
"operator": "AND"
}
}
}
}
但是,这将同时返回先生一& B先生:
{
"query": {
"match": {
"quotations": {
"query": "this these",
"operator": "AND"
}
}
}
}
使用您的运营商,等我管理。 thnks – 2017-01-09 10:32:41
我必须将值更改为:'{“name”:“Mr A”,“quotations”:[{“value”:“引用one,this and that和这些”},{“value”: “引用二,那些和那个”}]}',但这工作。有没有办法使用QueryStringQuery与此?我尝试使用一个(只是用query_string替换bool),它似乎没有工作。 – oneway 2014-10-09 13:26:29
你说得对。现在我注意到我拷贝了错误的值,即使我使用了正确的值(你提到的)。 – 2014-10-09 13:34:01
试试这个:'{ “查询”:{ “嵌套”:{ “路径”: “语录”, “查询”:{ “QUERY_STRING”:{ “default_field”: “quotations.value” “查询”: “这一点,这些” }} } } } ' – 2014-10-09 13:36:11