在弹性搜索中对数组元素进行查询字符串搜索

one*_*way 32 elasticsearch

我正在尝试使用一个简单的示例应用程序来学习elasticsearch,该应用程序列出了与人相关的引用.示例映射可能如下所示:

{ 
  "people" : {
    "properties" : {
      "name" : { "type" : "string"},
      "quotations" : { "type" : "string" }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

一些示例数据可能如下所示:

{ "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"]
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在个别引用上使用查询字符串api,并返回匹配的人.例如,我可能想要找到包含(这个和这些)的报价的人 - 应该返回"A先生"而不是"B先生",依此类推.我怎样才能做到这一点?

EDIT1:

Andrei在下面的回答似乎有效,数据值现在看起来像:

{"name":"Mr A","quotations":[{"value" : "quotation one, this and that and these"}, {"value" : "quotation two, those and that"}]}
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法使query_string查询工作.以下产生无结果:

{
  "query": {
    "nested": {
      "path": "quotations",
      "query": {
        "query_string": {
            "default_field": "quotations",
            "query": "quotations.value:this AND these"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让query_string查询使用嵌套对象?

编辑2:是的,见安德烈的回答.

And*_*fan 36

要实现该要求,您需要查看嵌套对象,而不是查询已展平的值列表,而是查询该嵌套对象中的各个值.例如:

{
  "mappings": {
    "people": {
      "properties": {
        "name": {
          "type": "string"
        },
        "quotations": {
          "type": "nested",
          "properties": {
            "value": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

价值观:

{"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"}]}
Run Code Online (Sandbox Code Playgroud)

查询:

{
  "query": {
    "nested": {
      "path": "quotations",
      "query": {
        "bool": {
          "must": [
            { "match": {"quotations.value": "this"}},
            { "match": {"quotations.value": "these"}}
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


eva*_*ong 6

不幸的是,没有好办法. 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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但这将使A先生和B先生都回归:

{
  "query": {
    "match": {
      "quotations": {
        "query": "this these",
        "operator": "AND"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)