elasticSearch-api的具体操作步骤讲解


Posted in Java/Android onJune 28, 2021

使用步骤

1.环境准备

用的是windows版,自行下载

链接: 下载地址

2.针对索引操作

这里是kibana上操作的(也可以用postman操作):

elasticSearch-api的具体操作步骤讲解

#创建索引,指定文档id
PUT /test1/type1/1
{
  "name":"张三",
   "age":30
  
}
#创建索引规则(类似数据库建表)
PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type":"text"
      },
      "age":{
        "type": "integer"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}
#获取索引的信息,properties类型
GET test2
#创建索引,properties不指定类型会有默认类型
#也可以用作修改,但是必须写上全部字段,不然会丢失未写字段
PUT /test3/_doc/1
{
  "name":"张三",
  "age":30,
  "birth":"1991-06-23"
}
GET test3
#查看es健康状态
GET _cat/health
#查看所有索引状态
GET _cat/indices?v
#修改
POST /test3/_doc/1/_update
{
  "doc":{
    "name":"李四"
  }
}

3.针对doc操作(增删改)

代码如下(示例):

#新增索引,并添加doc
POST /chen/user/1
{
  "name":"张三",
  "age":11,
  "desc":"一顿操作猛如虎,一看工资2500",
  "tags":["技术宅","温暖","直男"]
}
POST /chen/user/2
{
  "name":"李四",
  "age":12,
  "desc":"憨批",
  "tags":["渣男","旅游","交友"]
}
POST /chen/user/3
{
  "name":"王五",
  "age":13,
  "desc":"瓜怂",
  "tags":["靓女","旅游","美食"]
}
POST /chen/user/4
{
  "name":"刘六",
  "age":14,
  "desc":"锅盔",
  "tags":["衰仔","旅游","美食"]
}
#获取数据
GET chen/user/1
#更新数据
POST chen/user/1/_update
{
  "doc":{
    "name":"更新"
  }
}
#删除
DELETE chen/user/1
#条件查询,匹配度越高,_score(分值)越高
GET chen/user/_search?q=name:李
GET chen/user/_search?q=name:李四
#等价于上面
GET chen/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  }
}

4.针对doc操作(查)

查询1(示例):

#_source结果过滤(指定需要字段结果集)
#sort排序
#from-size分页(类似limit )
#注意:这个查询是不可以些多个字段的(我试过了)
GET chen/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "_source": ["name","age"],
   "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
  "from":0,
  "size":1
}
#多条件精确查询
#以下都是bool的二级属性
#must:必须
#should,满足任意条件
#must_not,表示不满足
GET chen/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "李四"
        }},
        {"match": {
          "age": 11
        }}
      ]
    }
  }
}
#过滤.注意filter是bool(多条件)的二级属性
GET chen/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "李四"
        }}
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}
#分词器依然有效
#多个条件空格隔开就行,只要满足其中一个,就会被逮到
GET chen/user/_search
{
  "query": {
    "match": {
      "tags": "男 技术"
    }
  }
}
#精确查询,结果只能为1,多条直接不显示
GET chen/user/_search
{
  "query": {
    "term": {
      "name": "李四"
    }
  }
}

查询2(示例):

#新建索引
PUT test4
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "desc":{
        "type": "keyword"
      }
    }
  }
}
#插入数据
PUT test4/_doc/1
{
  "name":"张三name",
  "desc":"张三desc"
}
PUT test4/_doc/2
{
  "name":"张三name2",
  "desc":"张三desc2"
}
#分词器查询(并不是查询索引里的数据,而是将text的内容用分词器拆分的结果)
GET _analyze
{
  "analyzer": "keyword",
  "text": ["张三name"]
}
GET _analyze
{
  "analyzer": "standard",
  "text": "张三name"
}
GET test4/_search
{
  "query": {
    "term": {
      "name": "张"
    }
  }
}
#==keyword不会被分词器解析==
GET test4/_search
{
  "query": {
    "term": {
      "desc": "张三desc"
    }
  }
}

查询3(示例):

PUT test4/_doc/3
{
  "t1":"22",
  "t2":"2020-4-6"
}
PUT test4/_doc/4
{
  "t1":"33",
  "t2":"2020-4-7"
}
#精确查询多个值
GET test4/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "t1": "22"
          }
        },
        {
          "term": {
            "t1": "33"
          }
        }
      ]
    }
  }
}
#highlight:高亮
#pre_tags,post_tags:自定义高亮条件,前缀后缀
GET chen/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'", 
    "post_tags": "</p>", 
    "fields": {
      "name":{}
    }
  }
}

5.java-api

索引操作:

public class ES_Index {
    private static final String HOST_NAME = "localhost";
    private static final Integer PORT = 9200;
    private static RestHighLevelClient client;

    //创建ES客户端
    static {
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(HOST_NAME, PORT));
        client = new RestHighLevelClient(restClientBuilder);
    }

    //关闭ES客户端
    public void close() {
        if (null != client) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //创建索引
    public void addIndex() throws IOException {
        //创建索引
        CreateIndexRequest request = new CreateIndexRequest("chen");
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //响应状态
        System.out.println("索引创建操作: " + response.isAcknowledged());
    }
    //查询索引
    public void selectIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("chen");
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        System.out.println("索引查询操作: " +response.getAliases());
        System.out.println("索引查询操作: " +response.getMappings());
        System.out.println("索引查询操作: " +response.getSettings());
    }
    //删除索引
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("chen");
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println("索引删除操作: "+response.isAcknowledged());
    }
    public static void main(String[] args) throws IOException {
        ES_Index index=new ES_Index();
        //index.addIndex();
        //index.selectIndex();
        index.deleteIndex();
        index.close();
    }
}

文档操作:

public class ES_Doc {
    private static final String HOST_NAME = "localhost";
    private static final Integer PORT = 9200;
    private static RestHighLevelClient client;

    //创建ES客户端
    static {
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(HOST_NAME, PORT));
        client = new RestHighLevelClient(restClientBuilder);
    }

    //关闭ES客户端
    public void close() {
        if (null != client) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //插入数据
    public void addDoc() throws IOException {
        IndexRequest request = new IndexRequest();
        User user = new User("张三", "男", 18);
        //向es插入数据,必须将数据转换为json格式
        String userJson = new ObjectMapper().writeValueAsString(user);
        request.index("user").id("1001").source(userJson, XContentType.JSON);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println("文档创建操作: " + response.getResult());
    }

    //修改数据(局部修改)
    public void updateDoc() throws IOException {
        UpdateRequest request = new UpdateRequest();
        request.index("user").id("1001").doc(XContentType.JSON, "sex", "女");
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("文档修改操作: " + response.getResult());
    }

    //获取数据
    public void getDoc() throws IOException {
        GetRequest request = new GetRequest();
        request.index("user").id("1001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        User user = new ObjectMapper().readValue(response.getSourceAsString(), User.class);
        System.out.println("文档获取操作: " + user);
    }

    //删除数据
    public void deleteDoc() throws IOException {
        DeleteRequest request = new DeleteRequest();
        request.index("user").id("1001");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println("文档删除操作: " + response.getResult());
    }

    //批量插入数据
    public void addBatch() throws IOException {
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "张三", "sex", "男", "age", 10));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "李四", "sex", "男", "age", 20));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "王五", "sex", "女", "age", 30));
        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "赵六", "sex", "男", "age", 40));
        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "孙七", "sex", "女", "age", 50));
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println("文档批量新增操作: " + response.getTook());
        System.out.println("文档批量新增操作: " + !response.hasFailures());//是否失败
    }

    //批量删除数据
    public void deleteBatch() throws IOException {
        BulkRequest request = new BulkRequest();
        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));
        request.add(new DeleteRequest().index("user").id("1004"));
        request.add(new DeleteRequest().index("user").id("1005"));
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println("文档批量删除操作: " + response.getTook());
        System.out.println("文档批量删除操作: " + !response.hasFailures());//是否失败
    }

    //查询(重点)
    public void searchDoc() throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        //1.查询索引中的全部数据
        //request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        //2.查询年龄为30的数据
        //request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30)));
        //3.分页查询,当前第0页,每页两条
        //request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).from(0).size(2));
        //4.排序,倒序
        //request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).sort("age", SortOrder.DESC));
        //5.过滤字段(排除和包含,也可以是数组)
        //request.source(new SearchSourceBuilder().fetchSource("name", null));
        //6.组合查询
        //BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //6.1 must相当于and
        //boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
        //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "女"));
        //6.2 should相当于or
        //boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
        //boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "女"));
        //request.source(new SearchSourceBuilder().query(boolQueryBuilder));
        //7.范围查询
        //request.source(new SearchSourceBuilder().query(QueryBuilders.rangeQuery("age").gte(30).lte(40)));
        //8.模糊查询Fuzziness.ONE即只差1个字符
        //request.source(new SearchSourceBuilder().query(QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE)));
        //9.高亮显示
        //SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchPhraseQuery("name", "张三"));
        //builder.highlighter(new HighlightBuilder().preTags("<font color='red'>").postTags("</font>").field("name"));
        //request.source(builder);
        //10.聚合查询
        //SearchSourceBuilder builder = new SearchSourceBuilder();
        //MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        //builder.aggregation(aggregationBuilder);
        //request.source(builder);
        //11.分组查询
        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(aggregationBuilder);
        request.source(builder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("--条数: " + hits.getTotalHits());
        System.out.println("--用时: " + response.getTook());
        hits.forEach((item)->{
            System.out.println("--数据: " + item.getSourceAsString());
        });
    }

    public static void main(String[] args) throws IOException {
        ES_Doc doc = new ES_Doc();
        //doc.addDoc();
        //doc.updateDoc();
        //doc.getDoc();
        //doc.deleteDoc();
        //doc.addBatch();
        //doc.deleteBatch();
        doc.searchDoc();
        doc.close();
    }
}

6.spring-data-elasticsearch

实体类: 关键在于@Document和@Field注解
shards 代表分片
replicas 代表副本

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "product", shards = 3, replicas = 1)
public class Product {
 @Id
 private Long id;//商品唯一标识
 @Field(type = FieldType.Text)
 private String title;//商品名称
 @Field(type = FieldType.Keyword)
 private String category;//分类名称
 @Field(type = FieldType.Double)
 private Double price;//商品价格
 @Field(type = FieldType.Keyword,index = false)
 private String images;//图片地址
}

dao层: 这样就已经可以了,类似mybatis-plus的BaseMapper,封装好了一些操作

@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}

yaml :不用怎么配置,默认就去找localhost:9200

测试 :不知道为啥dao的很多方法都过时了,看源码注释让回去用elasticsearchRestTemplate,感觉更繁琐

@SpringBootTest
class ElasticsearchApplicationTests {
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    ProductDao productDao;

    @Test
    void createIndex() {
        //创建索引,系统初始化会自动创建索引
        System.out.println("创建索引");
    }

    @Test
    void deleteIndex() {
        //创建索引,系统初始化会自动创建索引
        boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);
        System.out.println("删除索引 = " + flg);
    }

    //新增数据
    @Test
    void addDoc() {
        Product product = new Product();
        product.setId(1001L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("www.huawei.com");
        productDao.save(product);
    }

    //修改
    @Test
    void updateDoc() {
        Product product = new Product();
        product.setId(1001L);
        product.setTitle("小米手机");
        product.setCategory("手机");
        product.setPrice(4999.0);
        product.setImages("www.xiaomi.com");
        productDao.save(product);
    }

    //根据 id 查询
    @Test
    void findById() {
        Product product = productDao.findById(1001L).get();
        System.out.println(product);
    }

    //查询所有
    @Test
    void findAll() {
        Iterable<Product> products = productDao.findAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }

    //删除
    @Test
    public void delete() {
        productDao.deleteById(1001L);
    }

    //批量新增
    @Test
    public void saveAll() {
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId((long) i);
            product.setTitle("[" + i + "]小米手机");
            product.setCategory("手机");
            product.setPrice(1999.0 + i);
            product.setImages("http://www.atguigu/xm.jpg");
            productList.add(product);
        }
        productDao.saveAll(productList);
    }

    //分页查询
    @Test
    void findByPageable() {
        Sort orders = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(0, 5, orders);
        Page<Product> products = productDao.findAll(pageable);
        products.forEach(System.out::println);
    }

    /**
     * term 查询
     * search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
     */
    @Test
    void termQuery() {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Iterable<Product> products = productDao.search(termQueryBuilder);
        products.forEach(System.out::println);
    }

    /**
     * term 查询加分页
     */
    @Test
    void termQueryByPage() {
        PageRequest pageRequest = PageRequest.of(0, 5);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
        Iterable<Product> products = productDao.search(termQueryBuilder, pageRequest);
        products.forEach(System.out::println);
    }
}

到此这篇关于elasticSearch-api的具体操作步骤讲解的文章就介绍到这了,更多相关elasticSearch-api详解内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
ConstraintValidator类如何实现自定义注解校验前端传参
Jun 18 Java/Android
Java框架入门之简单介绍SpringBoot框架
Jun 18 Java/Android
详解java如何集成swagger组件
Jun 21 Java/Android
idea 在springboot中使用lombok插件的方法
Aug 02 Java/Android
logback 实现给变量指定默认值
Aug 30 Java/Android
MyBatis自定义SQL拦截器示例详解
Oct 24 Java/Android
零基础学java之带返回值的方法的定义和调用
Apr 10 Java/Android
Java数据结构之堆(优先队列)
May 20 Java/Android
springboot读取resources下文件的方式详解
Jun 21 Java/Android
java.util.NoSuchElementException原因及两种解决方法
Jun 28 Java/Android
MyBatis在注解上使用动态SQL方式(@select使用if)
Jul 07 Java/Android
Spring Boot实现文件上传下载
Aug 14 Java/Android
Java SSH 秘钥连接mysql数据库的方法
一篇文章带你复习java知识点
Java elasticsearch安装以及部署教程
Java 数组内置函数toArray详解
Java集成swagger文档组件
死磕 java同步系列之synchronized解析
Jun 28 #Java/Android
利用Java设置Word文本框中的文字旋转方向的实现方法
You might like
espresso double下 咖啡粉超细时 饼压力对咖啡的影响
2021/03/03 冲泡冲煮
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
PHP错误提示的关闭方法详解
2013/06/23 PHP
zf框架的db类select查询器join链表使用示例(zend框架)
2014/03/14 PHP
php像数组一样存取和修改字符串字符
2014/03/21 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
2016/09/14 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
Ext JS Grid在IE6 下宽度的问题解决方法
2009/02/15 Javascript
JavaScript中常见陷阱小结
2010/04/27 Javascript
javascript 密码强度验证规则、打分、验证(给出前端代码,后端代码可根据强度规则翻译)
2010/05/18 Javascript
js 数组的for循环到底应该怎么写?
2010/05/31 Javascript
jQuery为iframe的body添加click事件的实现代码
2011/04/07 Javascript
在服务端(Page.Write)调用自定义的JS方法详解
2013/08/09 Javascript
各种页面定时跳转(倒计时跳转)代码总结
2013/10/24 Javascript
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
javascript随机之洗牌算法深入分析
2014/06/07 Javascript
js动态切换图片的方法
2015/01/20 Javascript
jQuery+CSS实现简单切换菜单示例
2016/07/27 Javascript
jQuery简单实现点击文本框复制内容到剪贴板上的方法
2016/08/01 Javascript
Angular6 发送手机验证码按钮倒计时效果实现方法
2019/01/08 Javascript
vue 通过绑定事件获取当前行的id操作
2020/07/27 Javascript
HTML元素拖拽功能实现的完整实例
2020/12/04 Javascript
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
Python多线程爬虫简单示例
2016/03/04 Python
Python制作简易注册登录系统
2016/12/15 Python
Python运行不显示DOS窗口的解决方法
2018/10/22 Python
Django model 中设置联合约束和联合索引的方法
2019/08/06 Python
python使用 request 发送表单数据操作示例
2019/09/25 Python
Python性能分析工具py-spy原理用法解析
2020/07/27 Python
html5实现移动端适配完美写法
2017/11/16 HTML / CSS
搬家公司的创业计划书
2014/01/01 职场文书
服装电子商务创业计划书
2014/01/30 职场文书
总经理岗位职责范本
2015/04/01 职场文书
创业计划书之蛋糕店
2019/08/29 职场文书
《杜鹃的婚约》OP主题曲「凸凹」无字幕影像公开
2022/04/08 日漫
mysql字段为NULL索引是否会失效实例详解
2022/05/30 MySQL