gpt4 book ai didi

ElasticSearch - 在部分匹配之前精确排序?

转载 作者:行者123 更新时间:2023-12-02 22:20:55 25 4
gpt4 key购买 nike

在 ElasticSearch 中呈现部分匹配之前,我需要对精确的字符串匹配进行排序

假设我有三个文件:

  • 迈克尔 A
  • 迈克尔 B
  • 米查

  • 如果我搜索 micha ,它只返回第三个文档。但是,如果我搜索 micha* ,我按照上面列出的顺序获得了所有三个文件。

    我的问题是,如何在其他结果之前显示第三个文档(完全匹配)?
      :analysis => {
    :filter => {
    "my_edge_ngram_filter" => {
    "type" => "edgeNGram",
    "min_gram" => 1,
    "max_gram" => 128,
    "side" => "front",
    },
    "my_phone_filter" => {
    "type" => "pattern_replace",
    "pattern" => "\\D",
    "replacement" => ""
    }
    },
    :analyzer => {
    "default_index" => {
    "tokenizer" => "uax_url_email",
    "filter" => [
    "standard",
    "lowercase",
    "stop",
    "my_edge_ngram_filter"
    ],
    "type" => "custom"
    },
    "my_phone_analyzer" => {
    "tokenizer" => "keyword",
    "filter" => [
    "my_phone_filter",
    ],
    "type" => "custom"
    },
    "default_search" => {
    "type" => "standard"
    }
    }
    }
    }

    最佳答案

    您可以使用 multi_field + boosting:

  • 一个是按词搜索映射,并用高于第二个字段的分数来提升它
  • 其次是使用您的 ngram 分析器进行映射。

  • "name" : {
    "type" : "multi_field",
    "fields" : {
    "name" : {
    "type" : "string",
    "index" : "analyzed",
    "index_analyzer" : "standard",
    "search_analyzer" : "standard",
    "boost" : 10.0
    },
    "partial": {
    "type" : "string",
    "index" : "analyzed",
    "index_analyzer" : "your_ngram_analyzer",
    "search_analyzer" : "standard",
    "boost" : 1.0
    }
    }
    }


    然后在两个字段上进行搜索,例如:
    "query": {
    "query_string": {
    "query": "micha",
    "fields": [
    "name.partial",
    "name"
    ],
    "default_operator": "and"
    }
    }

    PS:您还可以在查询中动态指定字段提升,例如:
    "query": {
    "query_string": {
    "query": "micha",
    "fields": [
    "name.partial^1",
    "name^10"
    ],
    "default_operator": "and"
    }
    }

    关于ElasticSearch - 在部分匹配之前精确排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15673324/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com