模糊匹配得分高于完全匹配

时间:2017-12-16 06:47:26

标签: elasticsearch boto3 elasticsearch-5

我是ElasticSearch的新手,并且正在尝试配置Elasticsearch以给我模糊匹配。在实现模糊搜索,自动完成过滤器和带状疱疹时,精确匹配似乎具有比部分匹配更低的分数。例如,如果查询是" Ring",它似乎与" Brass Ring"而不是" Ring"。

任何人都可以帮助我吗?

以下是我制作索引的方法:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Words {
    public static void main(String[] args) throws IOException {
        Scanner key = new Scanner(System.in);
        File wordfile = new File("wrds.txt");
        if(wordfile.exists()){
            Scanner keyboard = new Scanner(wordfile);
            int counter = 0;
            System.out.println("Enter a charycter");
            char wrd = key.next().charAt(0);
            keyboard.useDelimiter(System.getProperty("line.separator"));
            while(keyboard.hasNext()) {
                String word = keyboard.next();
                if(word.startsWith(""+ wrd)) {
                    counter++;
                }
            }
            System.out.println("Found "+counter+" words that begin with "+ wrd);
        }
    }
}

以下是我查询术语的方法:

ToT dev branch

1 个答案:

答案 0 :(得分:1)

There is a very simple way to "boost" the score of exact matches: using a bool query that will use your already existent query and a term one inside should statements:

  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "fields": [
              "item_name",
              "item_id"
            ],
            "query": "Ring",
            "fuzziness": "AUTO"
          }
        },
        {
          "term": {
            "item_name.keyword": {
              "value": "Ring"
            }
          }
        }
      ]
    }
  }

And you'd have also to add a keyword type of subfield to the field that you want to favor a perfect match for:

  "mappings": {
    "my_type": {
      "properties": {
        "item_id": {
          "type": "string",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        },
        "item_name": {
          "type": "string",
          "analyzer": "autocomplete",
          "search_analyzer": "standard",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }