模糊查询构建器中是否有某种方法可以识别记录是否完 全匹配?

问题描述 投票:0回答:1

我有一些模糊查询,写成如下-

MatchQueryBuilder fuzzyQuery = QueryBuilders.matchQuery("color","blue color").fuzziness(Fuzziness.AUTO).fuzzyTranspoistions(true);

BoolQueryBuilder bool = new BoolQueryBuilder();

bool.should(fuzzyQuery);

SeachSourceBuilder search = new SearchSourceBuilder();

search.query(bool);

SearchRequest searchRequest = new SearchRequest(<index>,<type>)

searchRequest .source(search)

SearchResponse res = client.search(searchRequest,RequestOptions.DEFAULT);

'res'从弹性搜索返回一些记录,其中'color'列的值类似于字符串'blue color'。

现在有什么方法可以确定是否有任何结果记录包含字段'color'的值是'blue color'?

谢谢。

elasticsearch fuzzy-search
1个回答
0
投票

我建议将完全匹配查询添加到模糊查询中,并使用显着的提升因子来增强完全匹配查询,以使完全匹配脱颖而出。有点像

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "color": {
              "query": "blue color",
              "fuzziness": "auto",
              "fuzzy_transpositions": "true"
            }
          }
        },
        {
          "term": {
            "color.keyword": {
              "value": "blue color",
              "boost": 1000
            }
          }
        }
      ]
    }
  }
}

这会导致这样的结果

...
"hits" : [
  {
    "_index" : "so-score",
    "_type" : "_doc",
    "_id" : "IYG5C3AB1HeSr1rnK_bY",
    "_score" : 1205.3591,
    "_source" : {
      "color" : "blue color"
    }
  },
  {
    "_index" : "so-score",
    "_type" : "_doc",
    "_id" : "IoG5C3AB1HeSr1rnPfZA",
    "_score" : 1.2476649,
    "_source" : {
      "color" : "blue bolor"
    }
  },
...
© www.soinside.com 2019 - 2024. All rights reserved.