插入数据
DELETE /website
PUT /website
{
"mappings": {
"properties": {
"title": {
"type": "text"
}
}
}
}
PUT /website/_doc/1
{
"title": "first class"
}
PUT /website/_doc/2
{
"title": "second class"
}
PUT /website/_doc/3
{
"title": "third class"
}
GET /website/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"title": {
"order": "desc"
}
}
]
}
默认情况下在基于title 排序时会报错,解决方案:
1. title设置 fielddata=true
DELETE /website
PUT /website
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fielddata": true
}
}
}
}
PUT /website/_doc/1
{
"title": "first class"
}
PUT /website/_doc/2
{
"title": "second class"
}
PUT /website/_doc/3
{
"title": "third class"
}
GET /website/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"title": {
"order": "desc"
}
}
]
}
此时可以根据title倒序查询,但可以看出是通过title的第一个分词来排序的
但是想让整个title字段排序就需要
2. 将一个text字段建立两次索引,一个用于搜索一个用于排序
# 设置一个keyword类型的属性
PUT /website
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
# 查询时使用 title.keyword 查询 此时就是按照keyword进行排序
PUT /website/_doc/4
{
"title": "second art"
}
GET /website/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"title.keyword": {
"order": "asc"
}
}
]
}
标签:website,keyword,title,Text,字段,Elasticsearch,doc,PUT,class
From: https://www.cnblogs.com/l-zl/p/18158323