如何在Elasticsearch中区分字符串开头的匹配优先级? [英] How do prioritize matches in the beginning of strings in Elasticsearch?

查看:83
本文介绍了如何在Elasticsearch中区分字符串开头的匹配优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Elasticsearch实例,里面充满了包含电影和系列标题的文档.

运行此命令时:

  {询问": {布尔":{必须": [{"multi_match":{字段":[名称^ 2","SeriesName ^ 1.5",描述"],"fuzziness":"AUTO","prefix_length":2"query":游戏"}}]}}} 

...我得到了诸如大游戏",饥饿游戏",战争游戏"之类的标题

但是,我希望在标题仅包含"game"之前获得以"game"开头的标题.

当用户搜索游戏"时,他们希望在模仿游戏"之前出现权力的游戏"和游戏改变"之类的标题.

我该如何使它更精确?谢谢!

解决方案

尝试以下操作:

  {查询":{"prefix":{"Name":"game"}}} 

请参考相同的 Elasticsearch文档 的文档.>

为此,您的字段/属性必须被标记为关键字,请参见下面的查询.您还可以在映射中为您的字段/属性添加一个额外的小写过滤器.

  {设置":{指数": {分析": {分析器":{"analyzer_startswith":{"tokenizer":关键字","filter":小写"}}}}},映射":{"test_index":{特性": {名称": {"search_analyzer":"analyzer_startswith","index_analyzer":"analyzer_startswith","type":字符串"}}}}} 

I have an Elasticsearch instance full of documents containing movie and series titles.

When I run this:

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "fields": [
              "Name^2",
              "SeriesName^1.5",
              "Description"
            ],
            "fuzziness": "AUTO",
            "prefix_length": 2,
            "query": "game"
          }
        }
      ]
    }
  }
}

... I get titles like "The big game", "Hunger games", "War game", etc.

However, I would like to get titles starting with "game" BEFORE titles just containing "game".

When a user searches for "game", they expect titles like "Game of Thrones" and "Game change", before "The imitation game".

How can I make this more precise? Thank you!

解决方案

Try something like below :

{ "query": {
    "prefix" : { "Name" : "game" }
  }
}

Please refer the documentation for the same Elasticsearch Documentation

To do this your field/property have to be tokenized as a keyword, see query below. One can also add an additional lowercase filter in mapping for your field/property.

{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },
    "mappings": {
        "test_index": {
            "properties": {
                "Name": {
                    "search_analyzer": "analyzer_startswith",
                    "index_analyzer": "analyzer_startswith",
                    "type": "string"
                }
            }
        }
    }
}

这篇关于如何在Elasticsearch中区分字符串开头的匹配优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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