1. 数据准备
POST /liush/_bulk
{"index":{"_id":1}}
{ "title":"小米手机", "images":"http://image.jd.com/12479122.jpg", "price":1999, "stock": 200, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":2}}
{"title":"超米手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":3}}
{ "title":"小米电视", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "电视", "brand": "小米" } }
{"index":{"_id":4}}
{ "title":"小米笔记本", "images":"http://image.jd.com/12479122.jpg", "price":4999, "stock": 200, "attr": { "category": "笔记本", "brand": "小米" } }
{"index":{"_id":5}}
{ "title":"华为手机", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":6}}
{ "title":"华为笔记本", "images":"http://image.jd.com/12479122.jpg", "price":5999, "stock": 200, "attr": { "category": "笔记本", "brand": "华为" } }
{"index":{"_id":7}}
{ "title":"荣耀手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":8}}
{ "title":"oppo手机", "images":"http://image.jd.com/12479122.jpg", "price":2799, "stock": 400, "attr": { "category": "手机", "brand": "oppo" } }
{"index":{"_id":9}}
{ "title":"vivo手机", "images":"http://image.jd.com/12479122.jpg", "price":2699, "stock": 300, "attr": { "category": "手机", "brand": "vivo" } }
{"index":{"_id":10}}
{ "title":"华为nova手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
2. 匹配查询(match)
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
}
}
查询出很多数据,不仅包括`小米手机`,而且与`小米`或者`手机`相关的都会查询到,说明多个词之间是`or`的关系。
某些情况下,我们需要更精确查找,我们希望这个关系变成`and`,可以这样做:
GET /liush/_search
{
"query": {
"match": {
"title": {
"query": "小米手机",
"operator": "and"
}
}
}
}
3. 词条查询(term)
`term` 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。
GET /liush/_search
{
"query":{
"term":{
"price": 4999
}
}
}
4. 范围查询(range)
`range` 查询找出那些落在指定区间内的数字或者时间
`range` 查询允许以下字符:
操作符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
GET /liush/_search
{
"query":{
"range": {
"price": {
"gte": 1000,
"lt": 3000
}
}
}
}
5. 布尔组合(bool)
布尔查询 又叫 组合查询
`bool`把各种其它查询通过`must`(与)、`must_not`(非)、`should`(或)的方式进行组合
GET /liush/_search
{
"query":{
"bool":{
"must": [
{
"range": {
"price": {
"gte": 1000,
"lte": 3000
}
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 4000
}
}
}
]
}
}
}
注意:一个组合查询里面只能出现一种组合,不能混用
6. 过滤(filter)
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用`filter`方式
GET /liush/_search
{
"query": {
"bool": {
"must": {
"match": { "title": "小米手机" }
},
"filter": {
"range": {
"price": { "gt": 2000, "lt": 3000 }
}
}
}
}
}
注意:`filter`中还可以再次进行`bool`组合条件过滤。
7. 排序(sort)
`sort` 可以让我们按照不同的字段进行排序,并且通过`order`指定排序的方式
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": { "order": "desc" }
},
{
"_score": { "order": "desc"}
}
]
}
8. 分页(from/size)
from:从哪一条开始
size:取多少条
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"from": 2,
"size": 2
}
9. 高亮(highlight)
发现:高亮的本质是给关键字添加了<em>标签,在前端再给该标签添加样式即可。
- fields:高亮字段
- pre_tags:前置标签
- post_tags:后置标签
GET /liush/_search
{
"query": {
"match": {
"title": "小米"
}
},
"highlight": {
"fields": {"title": {}},
"pre_tags": "<em>",
"post_tags": "</em>"
}
}
10. 结果过滤(_source)
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在`_source`的所有字段都返回。
如果我们只想获取其中的部分字段,可以添加`_source`的过滤
GET /liush/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}