全文检索字段用match, 其他非text字段匹配用term。 不要使用term来进行文本字段查询
match_all query
POST /lagou-company-index/_search
{
"query": {
"match_all": {}
}
}
全文搜索(full-text query)
匹配搜索 match query
数据准备
PUT /lagou-property
{
"settings": {},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
POST /lagou-property/_doc/
{
"title": "小米电视4A",
"images": "http://image.lagou.com/12479122.jpg",
"price": 4288
}
POST /lagou-property/_doc/
{
"title": "小米手机",
"images": "http://image.lagou.com/12479622.jpg",
"price": 2699
}
POST /lagou-property/_doc/
{
"title": "华为手机",
"images": "http://image.lagou.com/12479922.jpg",
"price": 5699
}
or :
match
默认就是or关系POST /lagou-property/_search
{
"query": {
"match": {
"title": "小米电视4A"
}
}
}
and 分词后,多个词之间是and关系,
小米
、电视
、4A
POST /lagou-property/_search
{
"query": {
"match": {
"title": {
"query": "小米电视4A",
"operator": "and"
}
}
}
}
短语匹配
**match phrase query**
短语中的空格去掉后,不需要移动作为一个整体就能查询到 ```yaml GET /lagou-property/_search { “query”: { “match_phrase”: {
"title": "小米电视"
} } }
slop移动因子, 可以指定跨动分词个数
GET /lagou-property/_search { “query”: { “match_phrase”: { “title”: { “query”: “小米 4A”, “slop”: 2 } } } }
<a name="HWzL7"></a>
#### query string
无需指定某字段而对文档全文进行匹配查询的一个高级查询
```yaml
# 默认 全字段搜索
GET /lagou-property/_search
{
"query": {
"query_string": {
"query": "2699"
}
}
}
# 指定字段
GET /lagou-property/_search
{
"query": {
"query_string": {
"query": "2699",
"default_field": "price"
}
}
}
# 逻辑查询
GET /lagou-property/_search
{
"query": {
"query_string": {
"query": "手机 or 小米",
"default_field": "title"
}
}
}
# 模糊查询 ~1 表示带有修正的查询
GET /lagou-property/_search
{
"query": {
"query_string": {
"query": "大米~1",
"default_field": "title"
}
}
}
# 多字段支持
GET /lagou-property/_search
{
"query": {
"query_string": {
"query": "2699",
"fields": [
"title",
"price"
]
}
}
}
多字段匹配搜索 multi_match
GET /lagou-property/_search
{
"query": {
"multi_match": {
"query": "2699",
"fields": ["title","price"]
}
}
}
#可以用通配符来表示字段
GET /lagou-property/_search
{
"query": {
"multi_match": {
"query": "http://image.lagou.com/12479622.jpg",
"fields": [
"title",
"ima*"
]
}
}
}
词条级搜索 term_level queries
不分析词条,精确级搜索
PUT /book
{
"settings": {},
"mappings": {
"properties": {
"description": {
"type": "text",
"analyzer": "ik_max_word"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "float"
},
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
PUT /book/_doc/1
{
"name": "lucene",
"description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
"price": 100.45,
"timestamp": "2020-08-21 19:11:35"
}
PUT /book/_doc/2
{
"name": "solr",
"description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
"price": 100.45,
"timestamp": "2020-08-21 19:11:35"
}
PUT /book/_doc/3
{
"name": "Hadoop",
"description": "The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.",
"price": 620.45,
"timestamp": "2020-08-22 19:18:35"
}
PUT /book/_doc/4
{
"name": "ElasticSearch",
"description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力 的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条 款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜 索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢 迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。",
"price": 999.99,
"timestamp": "2020-08-15 10:11:35"
}
term query
用于查询指定字段包含某个词项的文档,类似于%xxx%
POST /book/_search
{
"query": {
"term": {
"name": "solr"
}
}
}
POST /book/_search
{
"query": {
"term": {
"description": "solr"
}
}
}
terms query
用于查询 指定字段 包含某些词项 ,类似于in %xxx%, %xxx%
GET /book/_search
{
"query": {
"terms": {
"name": [
"solr",
"elasticsearch"
]
}
}
}
范围搜索 range query
- gte:大于等于
- gt:大于
- lte:小于等于
- lt:小于
- boost:查询权重
```yaml
GET /book/_search
{
“query”: {
“range”: {
} } }"price": {
"gte": 10,
"lte": 200,
"boost": 2
}
GET /book/_search { “query”: { “range”: { “timestamp”: { “gte”: “now-10d/d”, “lt”: “now/d” } } } }
指定日期格式
GET book/_search { “query”: { “range”: { “timestamp”: { “gte”: “18/08/2020”, “lte”: “2021”, “format”: “dd/MM/yyyy||yyyy” } } } }
<a name="eFWzD"></a>
#### exists query 不为空搜索
相当于: column is not null
```yaml
GET /book/_search
{
"query": {
"exists": {
"field": "price"
}
}
}
词项前缀搜索(prefix query)
GET /book/_search
{
"query": {
"prefix": {
"name": "so"
}
}
}
通配符搜索(wildcard query)
GET /book/_search
{
"query": {
"wildcard": {
"name": "so*r"
}
}
}
GET /book/_search
{
"query": {
"wildcard": {
"name": {
"value": "lu*",
"boost": 2
}
}
}
}
模糊搜索(fuzzy query)
GET /book/_search
{
"query": {
"fuzzy": {
"name": "so"
}
}
}
#fuzziness模糊值
GET /book/_search
{
"query": {
"fuzzy": {
"name": {
"value": "so",
"boost": 1,
"fuzziness": 2
}
}
}
}
GET /book/_search
{
"query": {
"fuzzy": {
"name": {
"value": "sorl",
"boost": 1,
"fuzziness": 2
}
}
}
}
ids搜索(id集合查询)
GET /book/_search
{
"query": {
"ids": {
"values": [
"1",
"3"
]
}
}
}
复合搜索(compound query)
constant_score query
用来包装一个查询,将查询的文档评分设为常值
GET /book/_search
{
"query": {
"term": {
"description": "solr"
}
}
}
GET /book/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"description": "solr"
}
},
"boost": 1.0
}
}
}
布尔搜索(bool query)
bool 查询用bool操作来组合多个查询字句为一个查询。 可用的关键字
- must:必须满足
- should:或
- filter:必须满足
- must_not:必须不满足
```yaml
POST /book/_search
{
“query”: {
“bool”: {
} } }"should": {
"match": {
"description": "java"
}
},
"filter": {
"term": {
"name": "solr"
}
},
"must_not": {
"range": {
"price": {
"gte":300,
"lte":400
}
}
},
"minimum_should_match": 1,
"boost": 1
minimum_should_match代表了最小匹配精度,如果设置minimum_should_match=1,那么should 语句中至少需要有一个条件满足。
<a name="jG4xV"></a>
#### 排序
- 元数据分数,按相关性得分升序
```yaml
POST /book/_search
{
"query": {
"match": {
"description": "solr"
}
},
"sort": [
{
"_score": {
"order": "asc"
}
}
]
}
按字段排序
POST /book/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
多级排序
POST /book/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"timestamp": {
"order": "desc"
}
}
]
}
分页
POST /book/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
],
"size": 2,
"from": 2
}
高亮
POST /book/_search
{
"query": {
"match": {
"name": "solr"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [
{
"name": {}
}
]
},
"size": 2,
"from": 0
}
# 因为match的 字段是name,所以只能高亮name
POST /book/_search
{
"query": {
"match": {
"name": "elasticsearch"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [
{
"name": {}
},
{
"description": {}
}
]
}
}
# 匹配所有字段
POST /book/_search
{
"query": {
"query_string": {
"query": "elasticsearch"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [
{
"name": {}
},
{
"description": {}
}
]
}
}
文档批量操作
mget ```yaml
mget 不同索引
GET /_mget { “docs”: [ {
"_index": "book",
"_id": 1
}, {
"_index": "book",
"_id": 2
} ] }
mget同一个索引
GET /book/_mget { “docs”: [ { “_id”: 2 }, { “_id”: 3 } ] }
简化写法
POST /book/_search { “query”: { “ids”: { “values”: [ “1”, “4” ] } } }
- bulk
批量操作增删改
```yaml
POST /_bulk
{"delete":{"_index":"book","_id":"1"}}
{"create":{"_index":"book","_id":"5"}}
{"name":"test14","price":100.99}
{"update":{"_index":"book","_id":"2"}}
{"doc":{"name":"test"}}
- delete:删除一个文档,只要1个json串就可以了 删除的批量操作不需要请求体
- create:相当于强制创建 PUT /index/type/id/_create
- index:普通的put操作,可以是创建文档,也可以是全量替换文档
- update:执行的是局部更新partial update操作
格式:每个json不能换行。相邻json必须换行。 隔离:每个操作互不影响。操作失败的行会返回其失败信息。 一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(ES的 confifig下的elasticsearch.yml)中配置。 http.max_content_length: 10mb