一、ES基本增删改查
1、查询全部数据
GET /books/books/_search
2、查询某一条数据
# 注意 12 代表的是ES中的 _id
GET /books/books/12
3、更新某一条数据 (注意 : 字段需要写全 ,更新操作是将本条数据清除重新添加所给字段)
# 注意 12 代表的是ES中的 _id
POST /books/books/12/_update
{
"doc": {
"id": 12,
"title": "西游记",
"summary": "修改某一条数据"
}
}
4、新增一条数据
# 注意 12 代表的是ES中的 _id
PUT /books/books/12
{
"id": 12,
"title": "西游记",
"summary": "西游记",
}
5、删除一条数据
# 注意 12 代表的是ES中的 _id
DELETE /books/books/12
二、ES基本查询
1、普通关键词分词检索 默认根据匹配程度分数排序
GET /books/books/_search
{
"query": {
"match": {
"title": "社会保障制度与经济发展"
}
}
}
2、普通关键词检索之过滤字段 只查询title和pubdate字段
GET /books/books/_search
{
"query": {
"match": {
"title": "社会保障"
}
},
"_source": ["title","pubdate"]
}
3、普通关键词检索之排序 按照id倒排,正序是 asc
GET /books/books/_search
{
"query": {
"match": {
"title": "社会保障"
}
},
"_source": ["title","pubdate","id"],
"sort": [
{
"id": {
"order": "desc"
}
}
]
}
4、普通关键词检索之分页 “from”: 0,“size”: 20
GET /books/books/_search
{
"query": {
"match": {
"title": "社会保障"
}
},
"_source": ["title","pubdate","id"],
"sort": [
{
"id": {
"order": "desc"
}
}
],
"from": 0,
"size": 20
}
三、ES模糊查询
1、模糊查询- and 多个条件同时满足分词检索查询
相当于mysql 的 select * from books where title like “%西方经济学%” and publisher like “%河南科学技术出版社%”
GET /books/books/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "西方经济学"
}
},
{
"match": {
"publisher": "河南科学技术出版社"
}
}
]
}
}
}
2、模糊查询- or 多个条件满足其一分词检索查询
相当于mysql 的 select * from books where title like “%西方经济学%” or publisher like “%河南科学技术出版社%”
GET /books/books/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "河南科学技术出版社"
}
},
{
"match": {
"publisher": "河南科学技术出版社"
}
}
]
}
},
"from": 0,
"size": 20
}
3、模糊查询-不包括指定关键字检索 多个条件同时满足分词检索查询
GET /books/books/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"title": "河南科学技术出版社"
}
},
{
"match": {
"publisher": "河南科学技术出版社"
}
}
]
}
}
}
4、模糊查询-范围查询 并排序
GET /books/books/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "性爱的艺术"
}
}
],
"filter": {
"range": {
"id": {
"gt": 399995,
"lt": 400000
}
}
}
}
},
"sort": [
{
"id": {
"order": "desc"
}
}
]
}
四、精准查询
1、通过 GET /books 命令查看一下title的类型,发现是:text 还有keyword,这样的话做到完全匹配需要加上 title.keyword
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
GET /books/books/_search
{
"query": {
"term": {
"title.keyword": {
"value": "在共和国的旗帜下"
}
}
}
}
五、高亮查询
GET /books/books/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "南流河北流河"
}
}
]
}
},
"highlight": {
"pre_tags": [
"<em class=\"c_color\">"
],
"post_tags": [
"</em>"
],
"fields": {
"title":{}
}
}
}s