Elasticsearch核心知识篇(52)
初识搜索引擎_上机动手实战如何定位不合法的搜索以及其原因
_validate/query?explain
作用:可以检查我们的语句的合法性,以及出错的时候错误原因是什么
- 例程1
GET /waws_index/waws_type/_validate/query?explain
{
"query": {
"math": {
"test_field": "test"
}
}
}
{
"valid": false,
"error": "org.elasticsearch.common.ParsingException: no [query] registered for [math]"
}
- 例程2
GET /test_index/test_type/_validate/query?explain
{
"query": {
"match": {
"test_field": "test"
}
}
}
{
"valid": true,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"explanations": [
{
"index": "test_index",
"valid": true,
"explanation": "+test_field:test #(#_type:test_type)"
}
]
}
-
总结:
- 一般用在那种特别复杂庞大的搜索下,比如你一下子写了上百行的搜索,这个时候可以先用validate api去验证一下,搜索是否合法
Elasticsearch核心知识篇(53)
初识搜素引擎_上机动手实战如何定制搜索结果的排序规则
默认排序规则
默认情况下,是按照_score降序排序的
然而,某些情况下,可能没有有用的_score,比如说filter
当然,也可以是constant_score
GET /_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"author_id" : 1
}
}
}
}
}
定制排序规则
GET /waws_company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
},
"sort": [
{
"join_date": {
"order": "asc"
}
}
]
}
# 结果展示
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "waws_company",
"_type": "employee",
"_id": "2",
"_score": null,
"_source": {
"address": {
"country": "china",
"province": "shanxi",
"city": "xian"
},
"name": "marry",
"age": 35,
"join_date": "2015-01-01"
},
"sort": [
1420070400000
]
},
{
"_index": "waws_company",
"_type": "employee",
"_id": "1",
"_score": null,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "tom",
"age": 30,
"join_date": "2016-01-01"
},
"sort": [
1451606400000
]
},
{
"_index": "waws_company",
"_type": "employee",
"_id": "3",
"_score": null,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "waws",
"age": 37,
"join_date": "2016-01-01"
},
"sort": [
1451606400000
]
}
]
}
}
Elasticsearch核心知识篇(54)
初识搜索引擎_解密如何将一个field索引两次来解决字符串排序问题
-
问题引入
- 如果对一个string field进行排序,结果往往不准确,因为分词后是多个单词,再排序就不是我们想要的结果了
-
通常解决方案是,将一个string field建立两次索引,
- 一个分词,用来进行搜索;
- 一个不分词,用来进行排序
# 准备测试数据(正排索引)
PUT /weblalala
{
"mappings": {
"article": {
"properties": {
"title": {
"type": "text",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
},
"fielddata": true
},
"content": {
"type": "text"
},
"post_date": {
"type": "date"
},
"author_id": {
"type": "long"
}
}
}
}
}
PUT /website/article/1
{
"title": "first article",
"content": "this is my first article",
"post_date": "2017-01-01",
"author_id": 110
}
PUT /website/article/2
{
"title": "second article",
"content": "this is my second article",
"post_date": "2017-01-02",
"author_id": 110
}
PUT /weblalala/article/3
{
"title": "thrid article",
"content": "this is my thrid article",
"post_date": "2017-01-03",
"author_id": 110
}
- 查询全部数据,方便观察
GET /weblalala/article/_search
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "weblalala",
"_type": "article",
"_id": "2",
"_score": 1,
"_source": {
"title": "second article",
"content": "this is my second article",
"post_date": "2017-01-02",
"author_id": 110
}
},
{
"_index": "weblalala",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"title": "first article",
"content": "this is my first article",
"post_date": "2017-01-01",
"author_id": 110
}
},
{
"_index": "weblalala",
"_type": "article",
"_id": "3",
"_score": 1,
"_source": {
"title": "thrid article",
"content": "this is my thrid article",
"post_date": "2017-01-03",
"author_id": 110
}
}
]
}
}
- 使用排序
GET /website/article/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"title.raw": {
"order": "desc"
}
}
]
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "weblalala",
"_type": "article",
"_id": "3",
"_score": null,
"_source": {
"title": "thrid article",
"content": "this is my thrid article",
"post_date": "2017-01-03",
"author_id": 110
},
"sort": [
"thrid article"
]
},
{
"_index": "weblalala",
"_type": "article",
"_id": "2",
"_score": null,
"_source": {
"title": "second article",
"content": "this is my second article",
"post_date": "2017-01-02",
"author_id": 110
},
"sort": [
"second article" # 未分词,整体进行排序
]
},
{
"_index": "weblalala",
"_type": "article",
"_id": "1",
"_score": null,
"_source": {
"title": "first article",
"content": "this is my first article",
"post_date": "2017-01-01",
"author_id": 110
},
"sort": [
"first article"
]
}
]
}
}