目录:
1.安装Elasticsearch
2.Elasticsearch概念
3.Elasticsearch索引
4.Elasticsearch查询
5.Elasticsearch聚合aggregations
6.Spring Data Elasticsearch入门
7.Repository文档操作
现有数据
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "3",
"_score": 1,
"_source": {
"title": "apple手机",
"images": "3.jpg",
"price": 6899
}
}
]
}
}
基本语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
match_all
, match
,term
, range
等等match
类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系。
示例
GET /dxbindex1/_search
{
"query":{
"match":{
"title":"小米电视"
}
}
}
返回结果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 0.2876821,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
某些情况下,我们需要更精确查找,我们希望这个关系变成and
。
示例
GET /dxbindex1/_search
{
"query":{
"match":{
"title":{
"query": "小米电视",
"operator": "and"
}
}
}
}
返回结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
}
]
}
}
match
查询支持 minimum_should_match
最小匹配参数,我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数
。这里是向下取整的。比如有5个查询字符,5*75%=3.75,向下取整为3,也就是至少需要match 3个字符。示例
GET /dxbindex1/_search
{
"query":{
"match":{
"title":{
"query": "小米曲面电视",
"minimum_should_match": "75%"
}
}
}
}
返回结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
}
]
}
}
multi_match
与match
类似,不同的是它可以在多个字段中查询。
示例
GET /dxbindex1/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": [ "title", "subTitle" ]
}
}
}
返回结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 0.2876821,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 0.2876821,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
term
查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。
示例
GET /dxbindex1/_search
{
"query":{
"term":{
"price":2699.00
}
}
}
返回结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
terms
查询和 term
查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件。
示例
GET /dxbindex1/_search
{
"query":{
"terms":{
"price":[2699.00,2899.00,3999.00]
}
}
}
返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
}
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
示例
GET /dxbindex1/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
返回结果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手机"
}
}
]
}
}
示例
GET /dxbindex1/_search
{
"_source": {
"includes":["title","price"]
},
"query": {
"term": {
"price": 2699
}
}
}
与下面结果一样
GET /dxbindex1/_search
{
"_source": {
"excludes": ["images","saleable","stock"]
},
"query": {
"term": {
"price": 2699
}
}
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手机"
}
}
]
}
}
bool
把各种其它查询通过must
(与)、must_not
(非)、should
(或)的方式进行组合。
示例
GET /dxbindex1/_search
{
"query":{
"bool":{
"must": {
"match": {
"title": "小米" }},
"must_not": {
"match": {
"title": "电视" }},
"should": {
"match": {
"title": "手机" }}
}
}
}
返回结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 0.5753642,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
示例
range
查询找出那些落在指定区间内的数字或者时间。
GET /dxbindex1/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
返回结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
range
查询允许以下字符:
操作符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
fuzzy
查询是 term
查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2。
示例
GET /dxbindex1/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "3",
"_score": 0.6931472,
"_source": {
"title": "apple手机",
"images": "3.jpg",
"price": 6899
}
}
]
}
}
通过fuzziness
来指定允许的编辑距离:
GET /dxbindex1/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":0
}
}
}
}
返回结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter
方式。
注意:filter
中还可以再次进行bool
组合条件过滤。
示例
GET /dxbindex1/_search
{
"query":{
"bool":{
"must":{
"match": {
"title": "小米手机" }},
"filter":{
"range":{
"price":{
"gt":2000.00,"lt":3800.00}}
}
}
}
}
返回结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.87546873,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 0.87546873,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score
取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
示例
GET /dxbindex1/_search
{
"query":{
"constant_score": {
"filter": {
"range":{
"price":{
"gt":2000.00,"lt":3000.00}}
}
}
}
}
返回结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
}
}
]
}
}
sort
可以让我们按照不同的字段进行排序,并且通过order
指定排序的方式。
示例
GET /dxbindex1/_search
{
"query": {
"match": {
"title": {
"query" :"小米",
"operator": "and"
}
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": null,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
},
"sort": [
3999
]
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": null,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
},
"sort": [
2699
]
}
]
}
}
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序。
示例
GET dxbindex1/goods/_search
{
"query":{
"bool":{
"must":{
"match": {
"title": "小米手机" }},
"filter":{
"range":{
"price":{
"gt":2000,"lt":4000}}
}
}
},
"sort": [
{
"price": {
"order": "desc" }},
{
"_score": {
"order": "desc" }}
]
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "2",
"_score": 0.2876821,
"_source": {
"title": "小米电视",
"images": "2.jpg",
"price": 3999,
"stock": 200,
"saleable": true
},
"sort": [
3999,
0.2876821
]
},
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_score": 0.87546873,
"_source": {
"title": "小米手机",
"images": "1.jpg",
"price": 2699,
"stock": 100,
"saleable": true
},
"sort": [
2699,
0.87546873
]
}
]
}
}