【问题标题】:ElasticSearch adjacent words for nested queries用于嵌套查询的 ElasticSearch 相邻词
【发布时间】:2021-09-09 01:05:07
【问题描述】:

我正在使用 ES 7.14/Kibana 7.10,我必须搜索相邻的单词(任何顺序),因此我使用这个查询:

{
 "query":{
    "bool":{
        "must":[
            {
                "query_string":{
                    "query":"*antonio* *banderas*",
                    "fields":[
                        "text"
                    ],
                    "default_operator":"and",
                }
            }]
      }
  }
}

这适用于 text 普通字段。现在,我有一个嵌套字段metadata,假设映射是

{
    "mappings:": {
        "properties": {
            "text": {
                "type": "text"
            },
            "metadata": {
                "type": "nested",
                "properties": {
                    "text": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

并且我想以相同的方式搜索该嵌套字段(相邻词搜索),因此假设可以以 this 方式为 query_string 编写嵌套查询

{
  "query": {
    "query_string": {
      "query": "metadata.text:*antonio* *banderas*"
    }
  }
}

如何使用default_operator=and 等使这种方法适应以前的方法?如果我这样做了

   {
      "query": {
        "query_string": {
          "query": "metadata.text:*antonio* *banderas*",
          "default_operator": "and"
        }
      }
    }

我没有得到任何结果(但也没有任何错误)。

一个类似的问题,但与匹配多个嵌套字段的相邻单词有关的是here

【问题讨论】:

  • 对于嵌套字段,您需要使用嵌套查询,对于普通查询,它将被视为纯文本,即这些不会被视为单独的文档

标签: elasticsearch elasticsearch-7


【解决方案1】:

不应该使用query_string,而是使用wildcardmatchtermspan_term 搜索具有任何顺序的相邻单词

还有一个映射类型wildcard 对此进行了优化,具体取决于您需要的查询类型。

所以你的第一个例子:

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "text": "*antonio*"
          }
        },
        {
          "wildcard": {
            "text": "*banderas*"
          }
        }
      ]
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "text": "*antonio*banderas*"
          }
        }
      ]
    }
  }
}

对于嵌套查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "metadata",
            "query": {
              "bool": {
                "must": [
                  {
                    "wildcard": {
                      "metadata.text": "*antonio*"
                    }
                  },
                  {
                    "wildcard": {
                      "metadata.text": "*banderas*"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

【讨论】:

  • 谢谢。由于我的搜索字段类型是text,我可以应用wildcard 查询吗?如果没有,您能否提供termmatch 的替代示例?
  • 是的,您可以在 text 字段上使用通配符,只需记住 text 字段类型在标记中拆分短语,因此就像有一堆术语而不是短语一样,看到这个,使用 POST /myindice/_analysis {"text": "my phrase is cool"} 看看它是如何工作的
  • 谢谢。只是一个注释,但意义重大。出于某种原因我忽略了,它只适用于match 字段和query_string,其中前者(match)是后者(!)的 8 倍; wildcardterms 以这种方式使用不会返回任何结果 gist.github.com/loretoparisi/7383a912686c56428849efe4e553758a
  • 是的,很明显,就像我说的 text 类型将您的文本转换为一堆标记,因此 match 将尝试匹配其中一个标记,例如 my phrase 成为标记 myphrase 并且您不能使用 matchmy*phrase,因为它对于 my 令牌和 phrase 令牌都无效
  • 我有一个与匹配相邻单词相关的问题的扩展,但对于多个嵌套字段 - stackoverflow.com/questions/69107109/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 2018-03-08
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多