【问题标题】:Querystring search on array elements in Elastic Search在 Elastic Search 中对数组元素进行查询字符串搜索
【发布时间】:2014-12-03 04:41:37
【问题描述】:

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

{ 
  "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,并返回匹配的人。例如,我可能想找到引用包含(this AND these)的人——应该返回“Mr A”而不是“Mr B”,等等。我怎样才能做到这一点?

编辑1:

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

{"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:是的,请参阅 Andrei 的回答。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

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

    【讨论】:

    • 我必须将值更改为:{"name":"Mr A","quotations":[{"value" : "quotation one, this and that and these"}, {"value" : "quotation two, those and that"}]},但这有效。有什么方法可以使用 QueryStringQuery 吗?我尝试使用一个(只是用 query_string 替换 bool),但它似乎不起作用。
    • 你是对的。现在我注意到我复制粘贴了错误的值,即使我用正确的值(你提到的那些)进行了测试。
    • 试试这个:{ "query": { "nested": { "path": "quotations", "query": { "query_string": { "default_field": "quotations.value", "query": "this AND these" } } } } }
    • 啊,我只是将 default_field 作为引号。这行得通,非常感谢!
    • 作为 ElasticSearch 6.2 的更新(至少),当您尝试在 mappint 中使用 string 类型时会发生错误。运行此错误是No handler for type [string] declared on field [value]。类似的有效类型现在是 textkeyword (elastic.co/guide/en/elasticsearch/reference/current/…)。
    【解决方案2】:

    不幸的是,没有好的方法可以做到这一点。 https://web.archive.org/web/20141021073225/http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/complex-core-fields.html

    当您从 Elasticsearch 获取文档时,任何数组都将在 与索引文档时的顺序相同。 _source 字段 您返回的包含与您完全相同的 JSON 文档 索引。

    但是,数组是索引的 — 可搜索 — 作为多值字段, 这是无序的。在搜索时,您不能参考“第一个 元素”或“最后一个元素”。而是将数组视为一袋 价值观。

    换句话说,它总是考虑数组中的所有值。

    这将只返回 Mr A

    {
      "query": {
        "match": {
          "quotations": {
            "query": "quotation one",
            "operator": "AND"
          }
        }
      }
    }
    

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

    {
      "query": {
        "match": {
          "quotations": {
            "query": "this these",
            "operator": "AND"
          }
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      如果启用了scripting,这应该可以工作:

      "script": {
         "inline": "for(element in _source.quotations) { if(element == 'this' && element == 'these') {return true;} }; return false;"
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 2018-08-17
        • 2020-08-21
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        相关资源
        最近更新 更多