elasticsearch - ElasticSearch 6.8 不首先按完全匹配排序
问题描述
几天来我一直在寻找这种问题,但我没有成功。我遵循了这样的步骤,但没有成功。
所以基本上,我在 ElasticSearch 上有以下数据:
{ title: "Black Dust" },
{ title: "Dust In The Wind" },
{ title: "Gold Dust Woman" },
{ title: "Another One Bites The Dust" }
问题是我想按“灰尘”字搜索,我希望结果的排序如下:
{ title: "Dust In The Wind" },
{ title: "Black Dust" },
{ title: "Gold Dust Woman" },
{ title: "Another One Bites The Dust" }
其中“灰尘”必须出现在结果的顶部。
发布映射和查询比继续解释问题本身要好。
settings: {
analysis: {
normalizer: {
lowercase: {
type: 'custom',
filter: ['lowercase']
}
}
}
},
mappings: {
_doc: {
properties: {
title: {
type: 'text',
analyzer: 'standard',
fields: {
raw: {
type: 'keyword',
normalizer: 'lowercase'
},
fuzzy: {
type: 'text',
},
},
}
}
}
}
我的查询是:
"query": {
"bool": {
"must": {
"query_string": {
"fields": [
"title"
],
"default_operator": "AND",
"query": "dust"
}
},
"should": {
"prefix": {
"title.raw": "dust"
}
}
}
}
有人可以帮我吗?谢谢!
解决方案!
我想通了,我通过执行以下查询来解决:
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"prefix": {
"title.raw": {
"value": "dust",
"boost": 1000000
}
}
},
{
"match": {
"title": {
"query": "dust",
"boost": 50000
}
}
},
{
"match": {
"title": {
"query": "dust",
"boost": 10,
"fuzziness": 1
}
}
}
]
}
}
}
}
但是,在编写测试时,我发现了一个小问题。因此,我正在生成一个随机数uuid
并将以下内容添加到数据库中:
{ title: `${uuid} A` }
{ title: `${uuid} W` }
{ title: `${uuid} Z` }
{ title: `A ${uuid}` }
{ title: `z ${uuid}` }
{ title: `Z ${uuid}` }
当我执行上面的查询以查找 时uuid
,我得到:
uuid Z
uuid A
uuid W
Z uuid
我实现了我的第一个目标,即获得uuid
第一的位置,但为什么 Z 在 A 之前?(第一个和第二个结果)
解决方案
当其他一切都失败时,您可以使用一个简单的子字符串位置排序,如下所示:
{
"query": {
"bool": {
"must": {
...
},
"should": {
...
}
}
},
"sort": [
{
"_script": {
"script": "return doc['title.raw'].value.indexOf('dust')",
"type": "number",
"order": "asc" <--
}
}
]
}
我将顺序设置为,asc
因为子字符串索引越低,“分数”就越高。
编辑
我们必须考虑index == -1
所以将上面的脚本替换为:
"script": "def pos = doc['title.raw'].value.indexOf('dust'); return pos == -1 ? Integer.MAX_VALUE : pos"
推荐阅读
- listview - Listview中的奇怪字符
- html - 如何在移动视图中将 2 个引导轮播并排放置?
- python-3.x - 无法使用 AWS SNS 向印度 (+91) 发送短信
- python - 用户输入在 pandas DataFrame 中不起作用
- java - 我如何才能读取每个空单元格周围有多少地雷。游戏扫雷
- c# - 如何在 ASP.NET 中显示警报消息并重定向到主页
- python - SQLite 如何根据自定义规则对数据进行分组
- spring-boot - 非空引用空值或瞬态值
- c++ - FFMpeg C++ 如何创建具有多个输出的过滤器?
- c# - 使用 C# Windows 10 iot core 将文件复制到 USB