【问题标题】:ElasticSearch - Ordering exact before partial matches?ElasticSearch - 在部分匹配之前准确排序?
【发布时间】:2023-03-22 21:02:01
【问题描述】:

在 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"
        }
      }
    }
  }

【问题讨论】:

  • 你能把你正在做的查询添加到你的问题中吗?目前尚不清楚您正在查询哪些字段以及如何查询。

标签: elasticsearch


【解决方案1】:

你可以使用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"
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2014-12-09
    • 2016-06-23
    • 2018-03-17
    • 2016-10-10
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多