Elasticsearch可以说是企业级搜索应用的最佳选择。相较于传统的关系型数据库,Elasticsearch更适合于存储海量数据并对其进行全文检索和数据分析(聚合)。同时ES适合进行结构化/非结构化查询。下面介绍Elasticsearch中的API。

Elasticsearch系列教程
- Elasticsearch教程之一:介绍
- Elasticsearch教程之二:索引,分词及映射
- Elasticsearch教程之三:API
- Elasticsearch教程之四:AWS OpenSearch
- Elasticsearch教程之五:Opensearch Node.js客户端的使用
- Elasticsearch教程之六:Python客户端的使用
- Elasticsearch教程之七:在OpenSearch中使用聚合实现Facet
Create新增数据
插入数据
其格式为:
POST /index/type/id
{
YOUR_JSON
}
例如:
POST testindex/_doc/2
{
"name":"Lucas"
}
也可以不指定id,让系统自动生成:
POST testindex/_doc
{
"name":"Andrew"
}
批量插入
POST _bulk
{ "index" : { "_index" : "testindex", "_id" : "11" } }
{ "name":"Amy", "title":"Miss" }
{ "index" : { "_index" : "testindex", "_id" : "12" } }
{ "name":"Lily", "title":"Miss" }
如果都是针对同一个索引的话,也可以这样:
POST /testindex/_bulk
{ "index" : { "_id" : "13" } }
{ "name":"Amy", "title":"Miss" }
{ "index" : { "_id" : "14" } }
{ "name":"Lily", "title":"Miss" }
如果想让系统自动生成id:
POST /testindex/_bulk
{ "index" : { } }
{ "name":"Amy", "title":"Miss" }
{ "index" : { } }
{ "name":"Lily", "title":"Miss" }
Retrieve获取特定文档
获取指定ID的文档:
GET testindex/_doc/1
在这里包含了ES的一些系统信息和我们真正想要的数据。如果指向返回用户数据:
GET testindex/_doc/IHPV9HkBkMWsw4mVQ-4F/_source
只检索文档中指定的字段:
GET testindex/_doc/IHPV9HkBkMWsw4mVQ-4F?_source=name,address
同时检索多个文档(根据id):
POST /testindex/_mget
{
"ids":["11","12","21"]
}
需要注意的是,如果某个文档不存在,在返回json中会被标明(“found” : false)。
Update更新
POST testindex/_doc/1
{
"name":"Tom"
}
局部更新:
POST /testindex/_doc/IHPV9HkBkMWsw4mVQ-4F/_update
{
"doc": {
"title": "Mr"
}
}
Delete删除
删除索引:
DELETE myindex
删除文档(_doc为ES关键字,非类型名:无需指定类型名):
DELETE myindex/_doc/id
Search查询
获取当前索引中所有文档:
GET tutorial/_search
或者:
GET /tutorial/_search
{
"query": {
"match_all":{}
}
}
需要注意的是,从ES6.x开始,不推荐在同一个index中使用多个type,因此在上面的搜索中,只能返回第一个被创建索引中的文档。
基本查询
查询所有文档:
GET /_search
查询指定索引的文档:
GET /your_index/_search
查询多个索引
GET /yourindex1,yourindex2/_search
GET /yourindex*/_search
查询特定字段:
GET /your_index/_search?q=title:Dr
查询指定文档,指定字段
获取一个文档数据:
GET /demo/doc/1
和solr类似,其”_source”字段中包含文档数据。
获取所有文档:
GET /demo/doc/_search
全文检索:
GET /demo/doc/_search?q=keyword
GET /demo/doc/_search?q=keyword&df=user&sort=address:asc&timeout=2s
查询参数
还可以添加很多参数:
GET /your_index/_search?q=keyword&sort=salary:desc&df=username&from=200&size=50&timeout=1s
- df: 当不指定查询字段时要使用的默认字段。如果不指定,则会查询所有字段。
- timeout: 超时时间
- term:单词查询,如果使用term查询hello world就相当于查询hello or world
- phrase:语句查询(不分词),查询Hello world就等同于查询“Hello world”
分组:通过括号指定搜索表达式
q=username:(tom OR jim)
q=username:(tom OR jim) AND surname=bucks
查看查询的profile:
GET your_index/_search?q=username:(tom OR jim)
{
“profile”:true
}
通配符
?*,需要注意通配符会对系统性能又负面影响
可用替代方案:
正则表达式:
q=username:/[bt]om/
模糊匹配:
q=username:tom~1
返回与tom只相差一个字符的词,比如:bom,tok,tim
相似度查询
q=usrename:tom~5
布尔操作符
AND, OR, NOT, +, - (+在URL中会被解析成空格,需要encoding成%2B才能正常使用)
q=username:(+tom -tommy)
区间查询
q=salary:[1000 TO 2000]
q=salary:>=1000
聚合
GET /demo/doc/_search
{
"aggs": {
"all_interests": {
"terms":{"field":"interest"}
}
}
}
其返回结果会统计每个指定字段值的统计信息:
"aggregations": {
"all_interests": {
"buckets": [
{
"key": "football",
"doc_count": 200
},
{
"key": "netball",
"doc_count": 300
},
{
"key": "swimming",
"doc_count": 100
}
]
}
}
Search(Request body)
把所有查询参数都放在request中,而不是放在URL中。
match模糊查询
GET tutorial/_search
{
"query": {
"match": {
"title":"Miss"
}
}
}
或者:
GET tutorial/_search
{
"query": {
"match": {
"title":{
"query":"Miss Dr",
"operator":"or"
}
}
}
}
match_phrase精确匹配查询
GET tutorial/_search
{
"query": {
"match_phrase": {
"address":"temp Road"
}
}
}
slop差异查询
slop:允许有中间有几个词的差异,比如下面的查询:
GET tutorial/_search
{
"query": {
"match_phrase": {
"address": {
"query":"123 Road",
"slop":1
}
}
}
}
会返回 123 Temp Road,因为123和Road之间只差了一个词。只不过,slop后面的值代表的是up to xx。比如:如果使用“slop”:5,代表的是123和Road之间可以是0-5个词都可以。
query_string多字段查询
GET tutorial/_search
{
"query": {
"query_string": {
"default_field": "address",
"query":"Road OR Avenue"
}
}
}
多字段查询:
GET tutorial/_search
{
"query": {
"query_string": {
"fields": ["home_address","office_address"],
"query":"Road OR Avenue"
}
}
}
range范围查询
GET tutorial/_search
{
"query": {
"range": {
"age": {
"gte":10,
"lte":20
}
}
}
}