如何在elasticsearch中实现这种类型的查询?

cbjzeqam  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(300)

我已经在索引中添加了这样一个文档

POST /analyzer3/books
{
  "title": "The other day I went with my mom to the pool and had a lot of fun"
}

然后我做这样的查询

GET /analyzer3/_analyze
{
  "analyzer": "english",
  "text": "\"The * day I went with my * to the\""
}

并成功返回先前添加的文档。
我的想法是使用引号,以便查询变得准确,但也可以使用通配符替换任何单词。谷歌有这个确切的功能,你可以搜索这样的查询,例如 "I'm * the university" 它将返回包含如下文本的页面结果 I'm studying in the university right now 等等。
不过,我想知道是否有其他方法可以做到这一点。
我主要担心的是,这似乎不适用于其他语言,如日语和汉语。我尝试了许多分析器和标记器,但都没有用。
任何回答都将不胜感激。

rt4zxlrg

rt4zxlrg1#

elasticsearch没有像google那样的开箱即用的搜索,但是你可以构建类似的东西。
假设当有人引用搜索文本时,他们想要的是匹配短语查询。基本上移除 \" 并将剩下的字符串作为短语搜索。

PUT test/_doc/1
{
  "title": "The other day I went with my mom to the pool and had a lot of fun"
}

GET test/_search
{
  "query": {
    "match_phrase": {
      "title": "The other day I went with my mom to the pool and had a lot of fun"
    }
  }
}

对于 * 越来越有趣了。你可以从中进行多个短语搜索并将它们组合起来。例子:

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "title": "The"
          }
        },
        {
          "match_phrase": {
            "title": "day I went with my"
          }
        },
        {
          "match_phrase": {
            "title": "to the"
          }
        }
      ]
    }
  }
}

或者你可以在短语搜索中使用slop。搜索查询中的所有词都必须存在(除非它们被标记器删除或作为停止词),但是匹配的短语中可以有其他词。在这里,我们可以用1个其他单词替换每个因此总共有2个slop。如果您希望每个的位置都有一个以上的单词,则需要选择更高的坡度:

GET test/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "The * day I went with my * to the",
        "slop": 2
      }
    }
  }
}

另一个选择可能是木瓦,但这是一个更先进的概念,我会从现在的基础开始。

6yoyoihd

6yoyoihd2#

标记化字段上的精确匹配并不是那么简单。最好把你的领域保存为 keyword 如果你有这样的要求。
另外, keyword 数据类型支持通配符查询,它可以帮助您进行通配符搜索。
所以只要创建一个 keyword 类型子字段。然后对其使用通配符查询。
您的搜索查询如下所示:

GET /_search
{
    "query": {
        "wildcard" : { 
            "title.keyword" :  "The * day I went with my * to the" 
         }
    }
}

在上面的查询中,假设 title 字段有一个子字段,名为 keyword 数据类型的 keyword .
更多关于通配符查询的信息可以在这里找到。
如果你还想在 text 数据类型,然后读取

相关问题