首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastic search中数组元素的查询串搜索

Elastic search中数组元素的查询串搜索
EN

Stack Overflow用户
提问于 2014-10-08 21:35:53
回答 3查看 89.4K关注 0票数 39

我正在尝试用一个简单的示例应用程序学习elasticsearch,它列出了与人相关的报价。示例映射可能如下所示:

代码语言:javascript
复制
{ 
  "people" : {
    "properties" : {
      "name" : { "type" : "string"},
      "quotations" : { "type" : "string" }
    }
  }
}

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

代码语言:javascript
复制
{ "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:

安德烈下面的答案似乎是有效的,现在的数据值如下所示:

代码语言:javascript
复制
{"name":"Mr A","quotations":[{"value" : "quotation one, this and that and these"}, {"value" : "quotation two, those and that"}]}

但是,我似乎不能让query_string查询工作。以下操作不会产生任何结果:

代码语言:javascript
复制
{
  "query": {
    "nested": {
      "path": "quotations",
      "query": {
        "query_string": {
            "default_field": "quotations",
            "query": "quotations.value:this AND these"
        }
      }
    }
  }
}

有没有办法让query_string查询与嵌套对象一起工作?

Edit2:是的,请看安德烈的回答。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-08 23:04:44

为了实现这一要求,您需要查看嵌套对象,而不是查询一个扁平化的值列表,而是查询该嵌套对象中的单个值。例如:

代码语言:javascript
复制
{
  "mappings": {
    "people": {
      "properties": {
        "name": {
          "type": "string"
        },
        "quotations": {
          "type": "nested",
          "properties": {
            "value": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

值:

代码语言:javascript
复制
{"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"}]}

查询:

代码语言:javascript
复制
{
  "query": {
    "nested": {
      "path": "quotations",
      "query": {
        "bool": {
          "must": [
            { "match": {"quotations.value": "this"}},
            { "match": {"quotations.value": "these"}}
          ]
        }
      }
    }
  }
}
票数 40
EN

Stack Overflow用户

发布于 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

代码语言:javascript
复制
{
  "query": {
    "match": {
      "quotations": {
        "query": "quotation one",
        "operator": "AND"
      }
    }
  }
}

但这将同时返回A先生和B先生:

代码语言:javascript
复制
{
  "query": {
    "match": {
      "quotations": {
        "query": "this these",
        "operator": "AND"
      }
    }
  }
}
票数 7
EN

Stack Overflow用户

发布于 2018-09-17 09:23:28

如果启用了scripting,这应该会起作用:

代码语言:javascript
复制
"script": {
   "inline": "for(element in _source.quotations) { if(element == 'this' && element == 'these') {return true;} }; return false;"
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26258292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档