弹性搜索结果与搜索键不匹配

时间:2020-08-05 04:20:16

标签: spring-boot elasticsearch spring-data-elasticsearch

我已经完成了对名称的索引。它保存着椅子,椅子等数据。

当我尝试使用“ cha”进行搜索时,它没有返回任何椅子。它返回的是“粗毛地毯”,我希望有椅子的文档都可以。

下面是我的代码:

BoolQueryBuilder rootBoolQuery = new BoolQueryBuilder();
MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery(searchKey).field(
    SEARCH_CATEGORY_NAME_FIELD);
multiMatchQueryBuilder.fuzziness(Fuzziness.TWO);
multiMatchQueryBuilder.type(Type.BEST_FIELDS);
rootBoolQuery.must(multiMatchQueryBuilder);

搜索请求:

{"query":{"bool":{"must":[{"multi_match":{"query":"cha","fields":["categoryname^1.0"],"type":"best_fields","operator":"OR","slop":0,"fuzziness":"2","prefix_length":0,"max_expansions":50,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"sort":[{"productcount":{"order":"desc"}}]}

搜索响应:

{
"took": 4,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 1,
        "relation": "eq"
    },
    "max_score": null,
    "hits": [
        {
            "_index": "category",
            "_type": "_doc",
            "_id": "331869",
            "_score": null,
            "_source": {
                "rootcategory": "Home",
                "categorycode": "Shag_Rugs",
                "categoryname": "        Shag Rugs",
                "categoryid": 331869,
                "parentcategory": "    Rugs",
                "productcount": 59
            },
            "sort": [
                59
            ]
        }
    ]
}

}

映射:

"categoryname": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }

我该如何解决?

谢谢, 斯里

1 个答案:

答案 0 :(得分:0)

您好,您可以将前缀查询用于您的用例,并让我知道它是否解决了您的目的。请进入Click here to get more info