Elastic Search 相关基本操作
1.创建一个索引
2.添加一条数据
http://${host}:${port}/[index]/[type]/[id] Post请求
id可有可无 ex: http://localhost:9200/users/user { "id": "1001", "name": "zs" }
返回结果:

3.更新一条数据,如下:
http://${localhost}:${port}/users/user/[_id]/_update Post请求 ex: http://localhost:9200/users/user/ 更新的json串如下,加上/_update那么是更新符合字段: { "doc":{ "id":"1002", "name":"zss" } } 默认是所有的字段全部都更新。更新所有字段的方式如下: 发送post请求 http://localhost:9200/users/user/[_id] { "age":79 }
4.删除一条数据
发出delete请求:
http://localhost:9200/[index]/[type]/[_id]
ex:
http://localhost:9200/users/user/gDFxZ20B691frFPh1XM8
如果删除不存在的数据,那么会出现Not Found,404状态

5.搜索数据
发起get请求,默认返回10条数据 http://localhost:port/[index]/[type]/[_id] ex:http://localhost:9200/users/user/gTGFZ20B691frFPhlnMe

根据关键字进行搜索,GET请求 http://localhost:port/[index]/[type]/_search?q=[field:value] ex: http://localhost:9200/users/user/_search?q=age:79

发送POST请求 进行复杂的查询 http://localhost:port/[index]/[type]/_search ex: http://localhost:9200/users/user/_search { "query": { "match": { "age": 79 } } }

查询大于或小于10岁的人 发送POST请求 http://localhost:port/[index]/[type] http://localhost:9200/users/user/_search { "query": { "bool": { "filter": { "range": { "age" : { "gt" : 30 } } } } } }

发送post请求,而且先查询大于6,且匹配name为zs的人 http://localhost:port/[index]/[type]/_search bool 过滤为true的进入集合,bool下又filter,进行过滤,must是必须的,match进行匹配的,name为匹配的值 { "query":{ "bool":{ "filter":{ "range":{ "age":{ "gt":5 } } }, "must": { "match": { "name": "zs" } } } } }

全文检索,发送POST请求。 http://localhost:port/[index]/[type]/_search { "query":{ "match" : { "name": "zs ww" } } }

6.高亮显示
POST请求, ex: http://localhost:port/users/user/_search 注意highlight高亮字段中必须是match字段中的key { "query":{ "bool": { "should": [ {"match": {"name": "zs ww"}}, {"match": {"id": "10001"}} ] } }, "highlight": { "fields": { "name": {} } } }


浙公网安备 33010602011771号