ElasticSearch 增删改查手册
1 查看es中有哪些索引
GET /_cat/indices?v
2 增加一个索引(库)
PUT /movie_index
3 删除一个索引
ES 是不删除也不修改任何数据
DELETE /movie_index
4 新增文档
PUT /movie_index/movie/1
{ "id":1,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang chen"}
]
}
如果之前没建过index或者type,es 会自动创建。
5 直接用id查找
GET movie_index/movie/1
6 修改—整体替换
和新增没有区别
PUT /movie_index/movie/3
{
"id":"3",
"name":"incident red sea",
"doubanScore":"5.0",
"actorList":[
{"id":"1","name":"zhang chen"}
]
}
7修改—某个字段
POST movie_index/movie/3/_update
{
"doc": {
"doubanScore":"7.0"
}
}
8 删除一个document
DELETE movie_index/movie/3
9 搜索type全部数据
GET movie_index/movie/_search
10 按条件查询(全部)
GET movie_index/movie/_search
{
"query":{
"match_all": {}
}
}
11 按分词查询
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red"}
}
}
12 按分词子属性查询
GET movie_index/movie/_search
{
"query":{
"match": {"actorList.name":"zhang"}
}
13 match phrase
GET movie_index/movie/_search
{
"query":{
"match_phrase": {"name":"operation red"}
}
}
14 fuzzy查询
GET movie_index/movie/_search
{
"query":{
"fuzzy": {"name":"rad"}
}
}
15 过滤–查询后过滤
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red"}
},
"post_filter":{
"term": {
"actorList.id": 3
}
}
}
16 过滤–查询前过滤(推荐)
GET movie_index/movie/_search
{
"query":{
"bool":{
"filter":[ {"term": { "actorList.id": "1" }},
{"term": { "actorList.id": "3" }}
],
"must":{"match":{"name":"red"}}
}
}
}
17 过滤–按范围过滤
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": {
"range": {
"doubanScore": {"gte": 8}
}
}
}
}
}
关于范围操作符:
gt 大于
lt 小于
gte 大于等于
lte 小于等于
18 排序
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red sea"}
}
, "sort": [
{
"doubanScore": {
"order": "desc"
}
}
]
}
19 分页查询
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
20 指定查询的字段
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"_source": ["name", "doubanScore"]
}
21 高亮
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red sea"}
},
"highlight": {
"fields": {"name":{} }
}
}
22 java客户端
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
23 复杂查询
先过滤,后查询
"query":{
"bool":{
"filter":[ {"term": { "actorList.id": "1" }},{"term": { "actorList.id": "3" }}],
"must":[{"match":{"name":"red"}}]
}
}
"query": {
"bool": {
"filter": [{"terms":{ "actorList.id": [1,3]}}] ,
"must": [{"match": {"name": "red"}}]
}
}
创建mapping
mappings movie properties
PUT gmall
{
"mappings": {
"SkuInfo":{
"properties": {
"id":{
"type": "keyword"
, "index": false
},
"price":{
"type": "double"
},
"skuName":{
"type": "text",
"analyzer": "ik_max_word"
},
"skuDesc":{
"type": "text",
"analyzer": "ik_smart"
},
"catalog3Id":{
"type": "keyword"
},
"skuDefaultImg":{
"type": "keyword",
"index": false
},
"skuAttrValueList":{
"properties": {
"valueId":{
"type":"keyword"
}
}
}
}
}
}
}
查询
GET gmall/SkuInfo/_search
{
"query": {
"bool": {
"filter": [{"terms":{ "skuAttrValueList.valueId": ["46","45"]}},{"term":{"catalog3Id":"61"}}],
"must": { "match": { "skuName": "小米" } }
}
},
"highlight": {
"fields": {"skuName":{}}
},
"sort":{
"hotScore":{"order":"desc"}},
"aggs": { "groupby_attr": {"terms": {"field": "skuAttrValueList.valueId" }}
}
}
Index index = new Index.Builder(null).index("").type("").id(null).build();
如果聚合查询出现以下报错,则通过下面命令解决
Err: fielddata is disabled on text fields by default. set fielddata=true on [aaaa]