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

时间:2013-03-28 02:40:05

标签: elasticsearch

我在订购ElasticSearch

中的部分匹配之前订购了确切的字符串匹配

假设我有三个文件:

  • Michael A
  • Michael 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"
        }
      }
    }
  }

1 个答案:

答案 0 :(得分:3)

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