问题描述
我在Elastic Search中遇到字母排序问题.
I am facing alphabetical sorting issue in Elastic Search.
我有一个Index:indexlive和一个具有以下映射的用户"类型:
I have a Index: indexlive and a "users" type with following mapping:
{
"liveindex": {
"mappings": {
"users": {
"properties": {
"user_Confirmed": {
"type": "boolean"
},
"user_DateOfBirth": {
"type": "text"
},
"user_Email": {
"type": "text",
"analyzer": "standard"
},
"user_Gender": {
"type": "text"
},
"user_Name": {
"type": "text",
"analyzer": "standard"
},
"user_Photo": {
"type": "text"
},
"user_UserID": {
"type": "keyword"
}
}
}
}
}
}
我的查询是这样:
{
"index": "liveindex",
"type": "users",
"body": {
"from": 0,
"size": 50,
"query": {
"query_string": {
"fields": [
"user_Name"
],
"default_operator": "AND",
"query": "neel"
}
},
"sort": {
"_score": {
"order": "desc"
}
}
}
}
在上述查询中运行时,我得到以下结果:
I am getting following results when I run above query:
Neel Chaudhary
Neel Chaudhary
ITZ NEEL
nei modi
尼尔·亚格尼克
anu neel
swapna neel
swapna neel
我希望查询首先返回以"Neel"或"neel"作为第一个单词的名称.但是正如您所看到的ITZ NEEL,anu neel超出了所需的顺序.我也应用了按user_Name排序,但没有成功.我知道弹性搜索使用的是字母顺序,而不是字母顺序.
I want the query to return those names first which have "Neel", or "neel" as first word. But as you can see the ITZ NEEL, anu neel is out of the desired order. I have applied sorting by user_Name also but there was no success.I know that Elastic search uses lexicographical order as opposed to alphabetical order.
有人可以帮我吗?
推荐答案
这不是字典顺序还是字母顺序的问题.您应该为 user_Name
字段创建一个 keyword
子字段,以便以后可以使用 ne
前缀来增强文档:
This is not an issue of lexicographical vs alphabetical order. You should create a keyword
sub-field for your user_Name
field so that you can later boost the documents with the neel
prefix:
PUT liveindex
{
"settings": { <-- add these settings
"analysis": {
"normalizer": {
"lowercase": {
"type": "custom",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"users": {
"properties": {
"user_Confirmed": {
"type": "boolean"
},
"user_DateOfBirth": {
"type": "text"
},
"user_Email": {
"type": "text",
"analyzer": "standard"
},
"user_Gender": {
"type": "text"
},
"user_Name": {
"type": "text",
"analyzer": "standard",
"fields": { <-- add this sub-field
"sort": {
"type": "keyword",
"normalizer": "lowercase"
}
}
},
"user_Photo": {
"type": "text"
},
"user_UserID": {
"type": "keyword"
}
}
}
}
}
然后,为您的数据建立索引,并最终使用以下查询,这将对具有以 neel
开头的 user_Name
字段的文档有所帮助:
Then, index you data and finally use the following query, which will give a slight boost to documents having a user_Name
field that starts with neel
:
POST liveindex/_search
{
"from": 0,
"size": 50,
"query": {
"bool": {
"must": {
"query_string": {
"fields": [
"user_Name"
],
"default_operator": "AND",
"query": "neel"
}
},
"should": {
"prefix": {
"user_Name.sort": "neel"
}
}
}
},
"sort": {
"_score": {
"order": "desc"
}
}
}
您将按以下顺序获取文档:
You'll get the documents in the following order:
- 尼尔·亚格尼克
- nei modi
- 乔尔哈里(Neel Chaudhary)
- ITZ NEEL
- anu neel
- swapna neel
这篇关于在弹性搜索5.6中如何对分析字段进行字母排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!