一,准备数据
1.创建索引
PUT /lagou-book/
2.创建mapping
-
PUT /lagou-book/doc/_mapping
-
{
-
"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"
-
}
-
}
-
}
3.数据准备
-
PUT /lagou-book/doc/1
-
{
-
"name": "lucene",
-
"description": "Lucene Core is a Java library providing powerful indexing",
-
"price":100.45,
-
"timestamp":"2020-08-21 19:11:35"
-
}
-
-
-
-
PUT /lagou-book/doc/2
-
{
-
"name": "solr",
-
"description": "Solr is highly scalable providing fully fault tolerant",
-
"price":320.45,
-
"timestamp":"2020-07-21 17:11:35"
-
}
-
-
-
PUT /lagou-book/doc/3
-
{
-
"name": "Hadoop",
-
"description": "The Apache Hadoop solr software library is a framework ",
-
"price":620.45,
-
"timestamp":"2020-08-22 19:18:35"
-
}
-
-
-
-
-
PUT /lagou-book/doc/4
-
{
-
"name": "ElasticSearch",
-
"description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力",
-
"price":999.99,
-
"timestamp":"2020-08-15 10:11:35"
-
}
4.查询
(1)词条搜索
term 用户查询某个字段包含某些词的文档
-
-
GET /lagou-book/_search
-
{
-
"query": {
-
"term": {
-
"name": "solr"
-
}
-
}
-
}
(2) terms query 用户指定字段包含多个词语的文档
-
-
GET lagou-book/_search
-
{
-
"query": {
-
"terms": {
-
"description": ["hadoop","elasticsearch"]
-
}
-
}
-
}
(3)range query 范围搜索
gte:大于等于; gt:大于; lte:小于等于 lt:小于;
boost:查询权重。boost可以将某个搜索条件的权重加大,此时当这个搜索条件和另一个搜索条件计算score时,搜索条件权重更大的document,score也会更高,当然也就会优先反回来。
-
GET lagou-book/_search
-
{
-
"query": {
-
"range": {
-
"price": {
-
"gte": 100,
-
"lte": 500
-
}
-
}
-
}
-
}
-
-
GET /lagou-book/_search
-
{
-
"query": {
-
"range": {
-
"timestamp": {
-
"gte": "18/08/2020",
-
"lte": "01/01/2021",
-
"format":"dd/MM/yyyy||yyyy"
-
}
-
}
-
}
-
}
(4)不为空搜索
查询指定字段不为空的文档
-
GET /lagou-book/_search
-
{
-
"query": {
-
"exists":
-
{
-
"field":"name"
-
}
-
}
-
}
(5) ids集合搜索
-
GET lagou-book/_search
-
{
-
"query": {
-
"ids": {
-
"type": "doc",
-
"values": [1,3]
-
}
-
}
-
}
(6)bool query布尔搜索
bool查询用bool操作来组合多个字段为一个查询字段,可用的字段:
must:必须满足
must:必须满足,但执行的是filter上下文,不参与、不影响评分
should:或
must_not:必须不满足
-
GET lagou-book/_search
-
{
-
"query": {
-
"bool": {
-
"must": [
-
{
-
"match": {
-
"description": "solr"
-
}
-
}
-
],
-
"should": {
-
"term": {
-
"name": "hadoop"
-
}
-
},
-
"must_not":{
-
"range":{
-
"price":{
-
"gte":300,
-
"lte":500
-
}
-
}
-
}
-
}
-
}
-
}
(7)排序
7.1 相关性评分排序
默认情况下,查询返回的结果是按照相关性排序的—最相关的文档排在前面。在es中,相关性由_score表示,默认排序是_score降序排。
-
GET lagou-book/_search
-
{
-
"query": {
-
"match": {
-
"description": "solr"
-
}
-
},
-
"sort": [
-
{
-
"_score": {
-
"order": "asc"
-
}
-
}
-
]
-
}
7.2多个字段排序(先按照价格降序排,再按照时间戳生序排)
-
GET lagou-book/_search
-
{
-
"query": {
-
"match_all": {}
-
},
-
"sort": [
-
{
-
"price": {
-
"order": "desc"
-
},
-
"timestamp":{
-
"order": "asc"
-
}
-
}
-
]
-
}
(8)分页
size:每页显示多少条
form: 当前页起始索引, int start = (pageNum - 1) * size
(9) 高亮显示
-
GET /lagou-book/_search
-
{
-
"query": {
-
"match": {
-
"description": "solr"
-
}
-
},
-
"highlight": {
-
"pre_tags": "<font color='pink'>",
-
"post_tags": "</font>",
-
"fields": [
-
{
-
"name": {}
-
},
-
{
-
"description": {}
-
}
-
]
-
}
-
}
pre_tags :前置标签;
post_tags:后置标签
fields:需要高亮的字段
(10)filter