操作Elasticsearch语句

本文详述了如何使用 Elasticsearch 进行索引管理、文档操作、映射设置以及各种查询方法,包括基本查询、高级查询如布尔查询、范围查询、模糊查询等。还展示了如何通过 SpringBoot 集成 Elasticsearch 进行数据操作,提供了一个全面的实践教程。
摘要由CSDN通过智能技术生成

操作Elasticsearch语句

GET _analyze
{
  "analyzer": "ik_smart",
  "text":"张三丰北京"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text":"狂神说Java"
}
POST /test1/type1/1
{
  "name":"木子",
  "age":20
}

#创建索引
PUT /ems
#查看所有索引
GET /_cat/indices?v
#删除索引
DELETE /ems

#映射操作
#创建映射 put /索引/类型 (一个索引中只能有一个类型)
#ems索引下创建emp类型,下面有id,name,age,bir
#7.0移上不支持索引指定类型,默认类型都是_doc
PUT /ems
{
  "mappings": {
    "properties":{
      "id":{
        "type":"keyword"
      },
      "name":{
        "type":"text"
      },
      "age":{
        "type":"integer"
      },
      "bir":{
        "type":"date"
      }
    }
  }
}
#查看映射
GET /ems
GET /ems/_mapping

#文档操作
#插入一条数据 PUT /索引/类型/文档id
POST /ems/_doc/
{
  "id":"1234567",
  "name":"张三2",
  "age":20,
  "bir":"2022-02-16"
}
#查看文档
#查看一条记录
GET /ems/_doc/YSrFAX8BjU_9Lrw7hSta

#根据id删除文档
DELETE /ems/_doc/1

#跟新文档
POST /ems/_doc/YSrFAX8BjU_9Lrw7hSta/_update
{
  "doc":{
    "name":"张三三"
  }
}

POST /ems/_update/YSrFAX8BjU_9Lrw7hSta
{
  "doc":{
    "name":"张三四"
  }
}

PUT /ems
{
  "mappings": {
    "properties": {
      "name":{"type":"text"},
      "age":{"type":"integer"},
      "bir":{"type":"date"},
      "content":{"type":"text"},
      "address":{"type":"keyword"}
    }
  }  
}
#批量添加文档
PUT /ems/_bulk
{"index":{}}
{"name":"小黑","age":23,"bir":"2021-01-22","content":"为开发团队选择一款优秀的MVC框架是一件难事儿,在众多的可行方案中抉择需要很高的经验水平","address":"北京"}
{"index":{}}
{"name":"王小黑","age":25,"bir":"2020-11-02","content":"Spring框架是一个分层架构,由7个定义良好的模块组成,Spring模块构建在核心容器之上,核心容器定义了创建、配置和管理bean的方式","address":"上海"}
{"index":{}}
{"name":"张小五","age":8,"bir":"2009-08-29","content":"springcloud作为java语言的微服务框架,他依赖于springboot,有快速开发,持续交付和容易部署的特点,springcloud的组件非常多,并在spring社区和netflix、pivotal两大公司推动下越来越完善","address":"无锡"}
{"index":{}}
{"name":"win7","age":9,"bir":"2013-01-26","content":"spring的目标是致力于全方位的简化java开发,这势必会引出更多的解释,Spring是如何简化开发的?","address":"天津"}
{"index":{}}
{"name":"梅超风","age":45,"bir":"1967-07-09","content":"redis是一个开源的使用c语言编写的、支持网络、可基于内存亦可持久化的日志型,key-value的非结构化数据库,提供多种语言的api","address":"杭州"}
{"index":{}}
{"name":"张无忌","age":28,"bir":"2021-01-22","content":"elasticsearch是基于Lucene的搜索服务器,提供了一个分布式、多用户的能力的全文检索引擎,基于restful风格接口","address":"北京"}

GET /ems/_doc/YiqDBX8BjU_9Lrw7HCta



#es中高级查询功能
#queryString方式(可用于简单检索)
#查询所有文档,排序,分页(不写分页es默认分页是10),_source返回指定字段
GET /ems/_search?q=*&sort=age:desc&from=2&size=2&_source=name,age

#queryDSL查询方式(重点)
#1查询所有
GET /ems/_search
{
  "query":{
    "match_all": {}
  }
}
#2.查询所有并排序
GET /ems/_search
{
  "query":{
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    },
    {
      "bir": {
        "order": "asc"
      }
    }
  ]
}

POST /ems/_update/aCrgBn8BjU_9Lrw7Syu4
{
  "doc":{
    "address":"城北"
  }
}
#3.分页,from,size
GET /ems/_search
{
  "query":{
    "match_all": {}
  },
  "from": 0,
  "size": 2,
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}
#4.查询结果中返回指定字段"_source": ["name","age","content"]
GET /ems/_search
{
  "query":{
    "match_all": {}
  },
  "_source": ["name","age","content"]
}

#5重点:term查询,基于关键词查询
GET /ems/_search
{
  "query":{
    "term": {
      "content": {
        "value": "spring"
      }
    }
  }
}

GET /ems/_mapping
#es默认分词器是标准分词器,对于英文进行单词分词,对于中文进行单字分词
#es中只有text类型可以分词,其他类型不分词
GET /_analyze
{
  "text": ["java 是一个跨平台产品,他非常好"]
}
#6.范围查询range
GET /ems/_search
{
  "query":{
    "range": {
      "age": {
        "gte": 10,
        "lte": 30
      }
    }
  }
}

#7.基于关键词的前缀查询prefix
GET /ems/_search
{
  "query":{
    "prefix": {
      "content": {
        "value": "s"
      }
    }
  }
}

#8.通配符查询wildcard:?用来匹配一个字符,*用来匹配多个字符
GET /ems/_search
{
  "query":{
    "wildcard": {
      "name": {
        "value": "小*"
      }
    }
  }
}

#9.多id查询
GET /ems/_search
{
  "query":{
    "ids": {"values": ["ZiqDBX8BjU_9Lrw7HCtb","ZyqDBX8BjU_9Lrw7HCtb","YyqDBX8BjU_9Lrw7HCtb"]}
  }
}

#10.模糊查询fuzzy用来模糊查询含有指定关键字的文档,最大的模糊错误距离是0~2之间
GET /ems/_search
{
  "query":{
    "fuzzy": {
      "content": {"value": "spring"}
    }
  }
}

#11.bool查询 三个关键词:must:相当于&&,同时成立。should:成立一个就行,相当于||。must_not不能满足任何一个,相当于!
GET /ems/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "25"
            }
          }
        },
        {
          "term": {
            "address": {
              "value": "北京"
            }
          }
        }
      ]
    }
  }
}

#12.高亮查询highlight
GET /ems/_search
{
  "query":{
    "term": {
      "address": {
        "value": "北京"
      }
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"], 
    "fields": {
      "*":{}
    }
  }
}

#13.多字段查询multi_match,搜索比较智能
#如果搜索的字段分词,则会对query的值先进行分词再搜索,
#如果搜索的字段不分词,则用query的值得整体进行搜索
GET /ems/_search
{
  "query":{
    "multi_match": {
      "query": "北京",
      "fields": ["content","address","name"]
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"], 
    "fields": {
      "*":{}
    }
  }
}

#14.多字段分词查询query_string
#把每个单词或者中文以标准分词器进行分词之后对每个单词或者中文进行检索,主要检索字段中包含其中之一就能检索出来
GET /ems/_search
{
  "query":{
    "query_string": {
      "default_field": "content",
      "query": "elasticsearch一"
    }
  }
}
GET /ems/_search
{
  "query":{
    "query_string": {
      "fields": ["name","content"], 
      "query": "张三丰"
    }
  }
}
GET /ems/_search
{
  "query":{
    "query_string": {
      "fields": ["name","content"], 
      "query": "北京",
      "analyzer": "ik_smart"
    }
  }
}

#ik分词器
#在创建索引时要指定分词器,如果不指定,之后检索时用的都是默认的标准分词器
 DELETE /ems
#给text类型的字段指定分词器
PUT /ems
{
  "mappings": {
    "properties": {
      "name":{"type":"text","analyzer": "ik_max_word"},
      "age":{"type":"integer"},
      "bir":{"type":"date"},
      "content":{"type":"text","analyzer": "ik_max_word"},
      "address":{"type":"keyword"}
    }
  }  
}

GET /ems

#批量添加文档
PUT /ems/_bulk
{"index":{}}
{"name":"小黑","age":23,"bir":"2021-01-22","content":"为开发团队选择一款优秀的MVC框架是一件难事儿,在众多的可行方案中抉择需要很高的经验水平","address":"北京"}
{"index":{}}
{"name":"王小黑","age":25,"bir":"2020-11-02","content":"Spring框架是一个分层架构,由7个定义良好的模块组成,Spring模块构建在核心容器之上,核心容器定义了创建、配置和管理bean的方式","address":"上海"}
{"index":{}}
{"name":"张小五","age":8,"bir":"2009-08-29","content":"springcloud作为java语言的微服务框架,他依赖于springboot,有快速开发,持续交付和容易部署的特点,springcloud的组件非常多,并在spring社区和netflix、pivotal两大公司推动下越来越完善","address":"无锡"}
{"index":{}}
{"name":"win7","age":9,"bir":"2013-01-26","content":"spring的目标是致力于全方位的简化java开发,这势必会引出更多的解释,Spring是如何简化开发的?","address":"天津"}
{"index":{}}
{"name":"梅超风","age":45,"bir":"1967-07-09","content":"redis是一个开源的使用c语言编写的、支持网络、可基于内存亦可持久化的日志型,key-value的非结构化数据库,提供多种语言的api","address":"杭州"}
{"index":{}}
{"name":"张无忌","age":28,"bir":"2021-01-22","content":"elasticsearch是基于Lucene的搜索服务器,提供了一个分布式、多用户的能力的全文检索引擎,基于restful风格接口","address":"北京"}

GET /ems/_search
{
  "query":{
    "term": {
      "content": {
        "value": "框架"
      }
    }
  }
}

GET /_analyze
{
  "analyzer": "ik_max_word",
  "text":"蓝瘦香菇杠精碰瓷"
}
GET /_analyze
{
  "analyzer": "ik_max_word",
  "text":"尚硅谷"
}

#过滤查询filter,常见的过滤器类型有,range,term,terms,exists Filter
#exists filter过滤文档中存在指定字段,获取字段不为空的索引记录
GET /ems/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "框架"
            }
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 23
            }
          }
        }
      ]
    }
  }
}

GET /ems/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "term": {
            "name": {
              "value": "小黑"
            }
          }
        }
      ],
      "filter": [
        {
          "term": {
            "content": "mvc"
          }
        }
      ]
    }
  }
}

GET /ems/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "term": {
            "content": {
              "value": "架构"
            }
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "name": [
              "小黑",
              "梅超风"
            ]
          }
        }
      ]
    }
  }
}

GET /ems/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "exists": {
            "field": "name"
          }
        }
      ]
    }
  }
}

springboot操作ES
定义实体类


import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Data
@Document(indexName = "ems")
public class Emp {

    @Id
    private String id;

    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String name;
    @Field(type = FieldType.Integer)
    private Integer age;

    @Field(type = FieldType.Date)
    private Date bir;
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String content;
    @Field(type = FieldType.Keyword)
    private String address;
}

实现ElasticsearchRepository<Object,IdType>

import com.lyg.elasticsearch.entity.Emp;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface EmpRepository extends ElasticsearchRepository<Emp,String> {
}

接口


import com.lyg.elasticsearch.dao.EmpRepository;
import com.lyg.elasticsearch.entity.Emp;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping
public class ElasticsearchController {

    @Autowired
    RestHighLevelClient restHighLevelClient; //可以用于复杂搜索,和empRepository可以一起使用

    @Autowired
    EmpRepository empRepository; //可以做一般的增删改查,api简单。可以自动把实体类转换成json

    @PostMapping("testSave")
    public Emp testSave(@RequestBody Emp emp){
        emp.setId(UUID.randomUUID().toString());
        Emp save = empRepository.save(emp);
        return save;
    }

    @GetMapping("testSearchAll")
    public  SearchResponse testSearchAll() throws IOException {
        SearchRequest searchRequest = new SearchRequest("ems");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchSourceBuilder query = searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        return search;

    }

    @DeleteMapping("deleteDocument/{documentId}")
    public String deleteDocument(@PathVariable("documentId") String documentId) throws IOException {
        DeleteRequest request = new DeleteRequest("ems", documentId);
        DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        return "删除成功";
    }

    @GetMapping("testSearch")
    public  SearchHit[] testSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest("ems");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        SearchSourceBuilder query = searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        SearchSourceBuilder query = searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("content","框架")).filter(QueryBuilders.rangeQuery("age").gte(0).lte(23)));
//                .from(0)
//                .size(3).postFilter(QueryBuilders.rangeQuery("age").gte(10).lte(23));
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(search.getHits().getHits());
        SearchHit[] searchHit = search.getHits().getHits();
        return searchHit;

    }

    @PostMapping("testUpdate")
    public RestStatus testUpdate(@RequestParam("id") String id,@RequestParam("name") String name,
                                 @RequestParam("age") String age) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("ems",id);
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("name",name);
        stringObjectMap.put("age",age);
        updateRequest.doc(stringObjectMap, XContentType.JSON);
//        updateRequest.doc("{\"name\":"+name,"\"age\":"+age+"}",XContentType.JSON);
        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        RestStatus status = update.status();
        return status;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值