gpt4 book ai didi

java - 如何根据 Elasticsearch 中字段出现在左侧的距离对字段进行排序?

转载 作者:行者123 更新时间:2023-12-02 03:46:09 24 4
gpt4 key购买 nike

我尝试使用 Elasticsearch Java API 来搜索包含搜索词的字段的文档,然后根据该词在字段值中出现的左侧距离对结果进行排序。

这就是我创建索引的方式:

final CreateIndexRequest request = new CreateIndexRequest("index-name");
request.settings(Settings.builder().put("index.max_result_window", MAX_RESULTS));
client.admin().indices().create(request).actionGet();

这就是我创建映射的方式:

{
"part": {
"properties":{
"PartNumber":{
"type":"keyword"
}
}
}
}

这就是我查询 Elasticsearch 的方式:

final CreateIndexRequest request = new CreateIndexRequest("index-name");
request.settings(Settings.builder().put("index.max_result_window", MAX_RESULTS));
client.admin().indices().create(request).actionGet();
client.admin()
.indices()
.preparePutMapping("index-name")
.setType("part")
.setSource(source, XContentType.JSON).execute().actionGet();

final SearchResponse searchResponse = elasticClient.getClient()
.prepareSearch("index-name")
.setQuery(boolQuery().must(wildcardQuery("PartNumber", "*five*")))
.addSort("PartNumber", SortOrder.DESC)
.setTypes("part")
.get();

按以下顺序返回结果:

three five six four five four 11
five 11 three 2 three five four threefive
11 22 three fourfive five four
1 22 three four six five

但是,我希望它们的顺序是:

five 11 three 2 three five four threefive
three five six four five four 11
11 22 three fourfive five four
1 22 three four six five

它根据术语出现在值左侧的距离对它们进行排序,这可能吗?

最佳答案

首先,尽量不要使用wildcard询问。它们效率不高,特别是当您使用带有长文本的关键字字段时。

如果您使用通配符查询来匹配文本 four Five 中的 Five,则应使用 ngram tokenizer .

也就是说,计算分数时不考虑标记的位置。据我所知,没有一种简单的方法可以根据标记的位置来对文档进行排序。

一种解决方案可能是使用多个 span_first包含在带有不同end参数的should查询中的查询。出现在第一个位置的 token 将比出现在第二个位置的 token 匹配更多的查询,等等。

关于java - 如何根据 Elasticsearch 中字段出现在左侧的距离对字段进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com