目录:
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
}
}
]
}
}
1、基本查询
基本语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
- 例如:
match_all
,match
,term
,range
等等
- 例如:
- 查询条件:查询条件会根据类型的不同,写法也有差异。
1.1、查询所有(match_all)
1.2、匹配查询(match)
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
}
}
]
}
}
- took:查询花费时间,单位是毫秒
- time_out:是否超时
- _shards:分片信息
- hits:搜索结果总览对象
- total:搜索到的总条数
- max_score:所有结果中文档得分的最高分
- hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
- _index:索引库
- _type:文档类型
- _id:文档id
- _score:文档得分
- _source:文档的源数据
and关系
某些情况下,我们需要更精确查找,我们希望这个关系变成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
}
}
]
}
}