ElasticSearch41:初识搜索引擎_上机动手实战如何定制搜索结果的排序规则
2018-01-05 15:23
701 查看
1.默认排序规则
默认情况下,是按照_score降序排序的,然后在某些情况下,如filter,不计算_score,所以没有可用的_score
例子:
GET /test_index/test_type/_search
{
"query": {
"bool": {
"filter": {
"term": {
"test_field": "hello"
}
}
}
}
}
执行结果:
当然,还有constant_score
GET /test_index/test_type/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"test_field": "hello"
}
}
}
}
}
执行结果:
2.定制排序规则
例子:查询10到50岁的员工,并按照join_date正序排序
GET /company/employee/_search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 50
}
}
},
"sort": [
{
"join_date": "asc"
}
]
}
执行结果:
{
"took": 565,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "wenzhou",
"country": "china"
},
"name": "tom",
"age": 31,
"join_date": "2015-01-02"
},
"sort": [
1420156800000
]
},
{
"_index": "company",
"_type": "employee",
"_id": "2",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "hangzhou",
"country": "china"
},
"name": "jerry",
"age": 25,
"join_date": "2017-01-02"
},
"sort": [
1483315200000
]
},
{
"_index": "company",
"_type": "employee",
"_id": "3",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "ningbo",
"country": "china"
},
"name": "tiny",
"age": 18,
"join_date": "2018-01-02"
},
"sort": [
1514851200000
]
}
]
}
}
默认情况下,是按照_score降序排序的,然后在某些情况下,如filter,不计算_score,所以没有可用的_score
例子:
GET /test_index/test_type/_search
{
"query": {
"bool": {
"filter": {
"term": {
"test_field": "hello"
}
}
}
}
}
执行结果:
{ "took": 33, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
当然,还有constant_score
GET /test_index/test_type/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"test_field": "hello"
}
}
}
}
}
执行结果:
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }
2.定制排序规则
例子:查询10到50岁的员工,并按照join_date正序排序
GET /company/employee/_search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 50
}
}
},
"sort": [
{
"join_date": "asc"
}
]
}
执行结果:
{
"took": 565,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "wenzhou",
"country": "china"
},
"name": "tom",
"age": 31,
"join_date": "2015-01-02"
},
"sort": [
1420156800000
]
},
{
"_index": "company",
"_type": "employee",
"_id": "2",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "hangzhou",
"country": "china"
},
"name": "jerry",
"age": 25,
"join_date": "2017-01-02"
},
"sort": [
1483315200000
]
},
{
"_index": "company",
"_type": "employee",
"_id": "3",
"_score": null,
"_source": {
"address": {
"province": "zhejiang",
"city": "ningbo",
"country": "china"
},
"name": "tiny",
"age": 18,
"join_date": "2018-01-02"
},
"sort": [
1514851200000
]
}
]
}
}
相关文章推荐
- ElasticSearch40:初识搜索引擎_上机动手实战如何定位不合法的搜索以及其原因
- ElasticSearch39:初识搜索引擎_上机动手实战多搜索条件组合查询
- ElasticSearch48:初识搜索引擎_上机动手实战基于scroll技术滚动搜索大量数据
- ElasticSearch36:初识搜索引擎_快速上机动手实战Query DSL搜索语法
- ElasticSearch38:初识搜索引擎_上机动手实战常用的各种query搜索语法
- ElasticSearch42:初识搜索引擎_揭秘如何将一个field索引两次来解决字符串排序问题
- 如何让搜索引擎搜索结果更精准?
- 【腾讯云搜分享】如何通过干预搜索结果排序,实现产品运营的策略和目的
- Lucene实战(三)多Field搜索,并且对搜索结果进行过滤和[自定义]排序
- 导向恶意网站的搜索引擎搜索结果
- 根据查询的关键字,实现获取百度指定页数的搜索结果的信息(网页地址,标题,摘要,并有排序的说明)
- ElasticSearch33:初识搜索引擎_手动建立和修改mapping以及定制sting类型数据是否分词
- solr搜索结果按更新时间与关键字相关度排序
- 如何模拟苹果appstore的搜索结果,如何获取应用的描述信息
- 如何抓取google的搜索结果?
- Google搜索引擎如何运作:不会手动调整结果
- fastjson排序问题-如何改变fastjson的排序规则
- 如何做到在各大搜索引擎搜索自己的网页
- 如何在搜索结果出来之前,让页面显示“等待中。。。”