Elasticsearch学习第二篇--常用的几种搜索方式
一、Query String Search
类似HTTP的GET请求,参数拼接。
查询全部
get test_index/base/_search
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": 1,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "1",
"_score": 1,
"_source": {
"name": "xiaoming",
"age": 18,
"sex": "M"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "3",
"_score": 1,
"_source": {
"name": "xiaojiejie",
"age": 25,
"sex": "F"
}
}
]
}
}
这里为了学习方便,先不用中文了。
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据
条件查询
GET /test_index/base/_search?q=age:18&q=name:xiaomei&sort=age:desc
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": null,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F"
},
"sort": [
18
]
}
]
}
}
这种只适合比较简单的查询,比如根据id;那些复杂的查询不适用。一般不使用这种查询方法。
二、Query DSL
有点类似POST请求。可以用json的格式来构建查询语法,支持复杂的语法,更加强大。常用的就是此方法。
查询全部
GET /test_index/base/_search
{
"query": {
"match_all": {}
}
}
可以查得全部。
条件查询
GET /test_index/base/_search
{
"query": {
"match": {
"name": "xiaomei"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": null,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F"
},
"sort": [
18
]
}
]
}
}
三、Query Filter
对数据过滤
如下:查询10-20岁的女性
GET /test_index/base/_search
{
"query": {
"bool": {
"must": [
{"match": {
"sex": "F"
}}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
查询结果
过滤掉了25岁的xiaojiejie
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": null,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F"
},
"sort": [
18
]
}
]
}
}
四、Full-text Search 全文检索
我先加个兴趣属性,方便测试
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": 1,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F",
"interests": "music movie"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "1",
"_score": 1,
"_source": {
"name": "xiaoming",
"age": 18,
"sex": "M",
"interests": "sport movie"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "3",
"_score": 1,
"_source": {
"name": "xiaojiejie",
"age": 25,
"sex": "F",
"interests": "movie"
}
}
]
}
}
然后开始查询兴趣为movie的数据
GET /test_index/base/_search
{
"query": {
"match": {
"interests": "music movie"
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5753642,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": 0.5753642,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F",
"interests": "music movie"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "xiaoming",
"age": 18,
"sex": "M",
"interests": "sport movie"
}
},
{
"_index": "test_index",
"_type": "base",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "xiaojiejie",
"age": 25,
"sex": "F",
"interests": "movie"
}
}
]
}
}
可以看出查到3条记录,但score有差异。
先查看interests字段的type
GET /test_index/_mapping/base
结果
{
"test_index": {
"mappings": {
"base": {
"properties": {
"age": {
"type": "long"
},
"interests": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
可以看出为text类型,所以我们查询music movie时,会被拆解为music 、movie 。现在总共3条数据 满足情况如下:
name | interests | match |
---|---|---|
xiaomei | music movie | 2 |
xiaoming | sport movie | 1 |
xiaojiejie | movie | 1 |
所以匹配出3条数据,其中xiaomei的匹配度最高,score最大。
六、Phase Search 短语搜索
和全文检索不同,全部检索是拆解,然后倒排索引中搜索;短语检索是整个短语作为条件搜索。
GET /test_index/base/_search
{
"query": {
"match_phrase": {
"interests": "music movie"
}
}
}
结果:只有xiaomei匹配上
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": 0.5753642,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F",
"interests": "music movie"
}
}
]
}
}
六、Highlight search 高亮搜索
就是把一些字段高亮显示
GET /test_index/base/_search
{
"query": {
"match_phrase": {
"interests": "music movie"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}
结果:除了返回的数据外,另外返回了高亮显示的部分
“music movie”
{
"took": 56,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "test_index",
"_type": "base",
"_id": "2",
"_score": 0.5753642,
"_source": {
"name": "xiaomei",
"age": 18,
"sex": "F",
"interests": "music movie"
},
"highlight": {
"interests": [
"<em>music</em> <em>movie</em>"
]
}
}
]
}
}