本文继续上文(Elasticsearch 入门实战(3)--REST API 使用一(CAT,Index,Document,Ingest API))介绍 Elasticsearch REST API,相关的环境及软件信息如下:CentOS 7.6.1810、Elasticsearch 8.13.4。

1、Search APIs

1.1、Count API(查询文档数量)

语法:

GET /<target>/_count
  • 1.

样例:

curl -X GET 'http://10.49.196.33:9200/poet-index/_count'             #查询该索引的所有文档数量

curl -X GET 'http://10.49.196.33:9200/poet-index/_count?q=name:杜甫' #通过 Lucene 查询语法指定条件

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.33:9200/poet-index/_count' -d ' #通过 "Query DSL" 指定条件 
{
  "query": {
    "term": {
      "name.keyword": {
        "value": "杜甫"
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

1.2、Search API(查询文档)

语法:

GET /<target>/_search
GET /_search
POST /<target>/_search
POST /_search
  • 1.
  • 2.
  • 3.
  • 4.

1.2.1、query

1.2.1.1、term/terms 查询

term 查询不会对输入的内容进行分词处理,而是作为一个整体来查询。

A、查询单个词

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "term": {
      "name": {
        "value": "李白"
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

B、查询多个词

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "terms": {
      "name": ["李白", "杜甫"]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
1.2.1.2、range 查询

按照范围查询。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 35
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
1.2.1.3、exists 查询

查询对应字段不为空的数据。

curl -X GET -H 'Content-Type:application/json' 'http://10.1.101.64:9200/poet-index/_search' -d '
{
  "query": {
    "exists": {
      "field": "poems"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
1.2.1.4、match 相关查询

A、match

对输入的内容进行分词处理,再根据分词查询。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match": {
      "success": "理想主义"
    }
  },
  "from": 0,
  "size": 10,
  "sort": [{
    "name": {
      "order": "asc"
    }
  }]
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

B、multi_match

多字段进行匹配。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "multi_match": {
      "query": "太白",
      "fields": ["about", "success"]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

C、match_phrase

类似 match,需要满足以下条件:

1.文档的分词列表要包含所有的搜索分词列表
2.搜索分词次序要和文档分词次序一致
3.slop 参数控制着匹配到的文档分词最大间距,默认为1(匹配到分词要紧挨着)

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match_phrase": {
      "success": "文学作家"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

D、match_all

查询所有文档。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match_all": {
     }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

不加请求体,也是一样的效果,查询所有文档。

curl -X GET 'http://10.49.196.11:9200/poet-index/_search'
  • 1.

E、match_none

与 match_all 相反,返回 0 个文档。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match_none": {
     }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
1.2.1.5、query_string 查询

query_string 可以同时实现前面几种查询方法。

A、类似 match

curl -X GET  -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "default_field": "success",
      "query": "古典文学"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

B、类似 mulit_match

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "query": "古典文学",
      "fields": ["about", "success"]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

C、类似 match_phrase

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "default_field": "success",
      "query": "\"古典文学\""
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

D、带运算符查询,运算符两边的词不再分词

1、查询同时包含 ”文学“ 和 ”伟大“ 的文档

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "default_field": "success",
      "query": "文学 AND 伟大"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "fields": ["success"],
      "query": "文学 伟大",
      "default_operator": "AND"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

2、查询 name 或 success 字段包含"文学"和"伟大"这两个单词,或者包含"李白"这个单词的文档。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "query_string": {
      "query": "(文学 AND 伟大) OR 李白",
      "fields": ["name", "success"]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
1.2.1.6、simple_query_string 查询

类似 query_string,主要区别如下:

1、不支持AND OR NOT ,会当做字符处理;使用 + 代替 AND,| 代替OR,- 代替 NOT
2、会忽略错误的语法

查询同时包含 ”文学“ 和 ”伟大“ 的文档:

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "simple_query_string": {
      "fields": ["success"],
      "query": "文学 + 伟大"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
1.2.1.7、fuzzy 查询

模糊查询时使用的参数:

fuzziness

允许的最大编辑距离,默认不开启模糊查询,相当于 fuzziness=0。支持的格式

1、可以是数字(0、1、2)代表固定的最大编辑距离

2、自动模式,AUTO:[low],[high]

    查询词长度在 [0-low)范围内编辑距离为 0(即强匹配)

    查询词长度在 [low, high) 范围内允许编辑 1 次

    查询词长度 >high 允许编辑 2 次

prefix_length

控制两个字符串匹配的最小相同的前缀大小,也就是前 n 个字符不允许编辑,必须与查询词相同,默认是 0,大于 0 时可以显著提升查询性能

max_expansions

产生的最大模糊选项

transpositions

相邻位置字符互换是否算作 1 次编辑距离,全文查询不支持该参数

A、全文查询时使用模糊参数

先分词再计算模糊选项。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match": {
       "success": {
         "query": "古典文化",
         "fuzziness": 1,
         "prefix_length": 0,
         "max_expansions": 5
       }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

B、使用 fuzzy query

对输入不分词,直接计算模糊选项。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "fuzzy": {
      "success": {
        "value": "理想",
        "fuzziness": 1,
        "prefix_length": 0,
        "transpositions": true
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
1.2.1.8、wildcard 查询

wildcard 查询类似 SQL 语句中的 like;? 匹配一个字符,* 匹配多个字符。对于使用 wildcard 查询的字段建议字段类型设为 wildcard 类型。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "wildcard": {
      "name": "李*"
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
1.2.1.9、bool 查询

组合查询使用 bool 来组合多个查询条件。

条件

说明

must

同时满足

should

满足其中任意一个

must_not

同时不满足

filter

过滤搜索,不计算得分

A、查询 success 包含 “思想” 且 age 在 [20-40] 之间的文档:

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "bool": {
      "must": [{
        "simple_query_string": {
          "query": "思想",
          "fields": ["success"]
        }
      }, {
        "range": {
          "age": {
            "gte": 20,
            "lte": 40
          }
        }
      }]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

B、过滤出 success 包含 “思想” 且 age 在 [20-40] 之间的文档,不计算得分:

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "bool": {
      "filter": [{
        "simple_query_string": {
          "query": "思想",
          "fields": ["success"]
        }
      }, {
        "range": {
          "age": {
            "gte": 20,
            "lte": 40
          }
        }
      }]
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

1.2.2、aggs 查询

聚合查询类似 SQL 中的 group by 分组查询。

A、求和,类似 select sum(age) from poet-index

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "aggs": {
    "age_sum": {
      "sum": {
        "field": "age"
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

B、类似 select count distinct(age) from poet-index

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/test-index/_search' -d '
{
  "aggs": {
    "age_count": {
      "cardinality": {
        "field": "age"
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

C、数量、最大、最小、平均、求和

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "aggs": {
    "age_stats": {
      "stats": {
        "field": "age"
      }
    }
  },
  "size": 0
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

D、类似 select name,count(*) from poet-index group by name

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "aggs": {
    "name_terms": {
      "terms": {
        "field": "name"
      }
    }
  },
  "size": 0
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

E、类似 select name,age, count(*) from poet-index group by name,age

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "aggs": {
    "name_terms": {
      "terms": {
        "field": "name"
      },
      "aggs": {
        "age_terms": {
          "terms": {
            "field": "age"
          }
        }
      }
    }
  },
  "size": 0
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

F、类似 select avg(age) from poet-indexwhere name='李白'

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "name": "李白"
        }
      }
    }
  },
  "aggs": {
    "age_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

1.2.3、suggest 查询

如果希望 Elasticsearch 能够根据我们的搜索内容给一些推荐的搜索选项,可以使用推荐搜索。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "suggest": {
    "success_suggest": {
      "text": "思考",
      "term": {
        "field": "success",
        "analyzer": "ik_max_word",
        "suggest_mode": "always",
        "min_word_length":2
      }
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

推荐模式 suggest_mode:

 推荐模式

说明

popular

推荐词频更高的一些搜索

missing

当没有要搜索的结果的时候才推荐

always

无论什么情况下都进行推荐

1.2.4、highlight

对搜索结果中的关键字高亮显示。

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
  "query": {
    "match": {
      "success": "思想"
    }
  },
  "highlight": {
    "pre_tags": "<span color='red'>",  
    "post_tags": "</span>",        
    "fields": {            
      "success": {}
    }
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

 

详细的 Elasticsearch REST API 使用说明,请参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html。