弹性搜索中对数组元素的查询字符串搜索 [英] Querystring search on array elements in Elastic Search

查看:40
本文介绍了弹性搜索中对数组元素的查询字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"]
}

我希望能够在单个引用上使用查询字符串 api,并返回匹配的人.例如,我可能想找到引用包含(这个和这些)的人——应该返回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?

编辑 1:

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆