zoukankan      html  css  js  c++  java
  • Java操作ES

    一、ES基本概念

    1. 节点:Elastic是一个分布式数据库,每个数据库实例是一个节点Node,一台服务器上可以有多个Node,可以多台服务器协同工作

    2. 集群:一组节点Node构成一个集群Cluster

    3. 文档:存储数据的基本单元称为文档Document,使用json表示

    4. 索引:索引Index是一类文档的集合,ES会索引所有字段,经过处理后写入一个反向索引

    5. 类型:一个索引的逻辑分区叫类型Type

    6. 分词器:把text类型的数据进行分词,分词后按照分词建立索引,可以按照分词进行查询;默认分词器为ik,可以设置为中文分词器ik_max_word

    二、跟普通数据库mysql在数据结构上的区别 

    mysql:库->表->行->字段

    ES:索引->类型->文档->字段

    三、数据类型

    1.ES在5.*版本之后,把string字段设置为了过时字段,引入text,keyword字段

    2.text会自动分词,keyword不分词,因为全部字段分词,会浪费空间

    3.如果不在mapping中定义字段类型,java的String类型会自动映射成text & keyword两种

    4.如果ES中已经有数据,不支持修改字段类型

    a.可以删除原来索引,重新建立

    b.新建一个索引,把老数据迁移到新索引上面

    c.如果数据比较多或者比较重要,可以新增一个字段

    三、增删改查API

    1.新增或者更新,/index/type/id已存在就会更新

    put /index/type/id 

    {

    "field1":"value1",

    "field2":"value2"

    }

    2.删除

    delete /index/type/id

    3.查询

    term:精确匹配一个字段

    match:模糊查询或者分词查询一个字段

    wildcard:使用通配符进行查询

    四、java使用RestHighLevelClient操作ES

    1.引入jar包

          <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.7.1</version>
            </dependency>

    2.初始化

    @Configuration
    public class TaskEsTopicConfig {
        private static final int HTTP_PORT = 9200;
        private static final String IP = "aaa";
        private static final String SecurityUser = "aaa";
        private static final String SecurityPassword = "aaa";
    
    
        private static CredentialsProvider init() {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(SecurityUser, SecurityPassword));
            return credentialsProvider;
        }
    
        @Bean
        public RestClientBuilder restClientBuilder() {
            //配置权限验证
            RestClientBuilder restClientBuilder = RestClient.builder( new HttpHost(IP, HTTP_PORT, "http"))
                    .setHttpClientConfigCallback(httpClientBuilder ->
                            httpClientBuilder.setDefaultCredentialsProvider(init()));
            return restClientBuilder;
        }
    
    
        @Bean(name = "highLevelClient")
        public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
            return new RestHighLevelClient(restClientBuilder);
        }
    
    }

    3.写数据

    IndexRequest request = new IndexRequest();
    request.index(index).type(type).source(JSON.toJSONString(object), XContentType.JSON);
    bulkRequest.add(request);
    // 保存数据到ES
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

    4.读数据 BoolQueryBuilder的should/must,对应mysql的or/and

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 分页参数
    searchSourceBuilder.from(1);
    searchSourceBuilder.size(10);
    searchSourceBuilder.sort("_id", SortOrder.DESC);
    
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
    // 精确匹配
    .must(QueryBuilders.termQuery("field1","value1")
    // 关键字模糊+分词匹配       
    .must(QueryBuilders.multiMatchQuery("keyword","field1","field2","field3","field4","field5"));
    searchSourceBuilder.query(boolQueryBuilder); SearchRequest searchRequest = new SearchRequest(); searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    if (Objects.nonNull(searchResponse)) {
    total = searchResponse.getHits().totalHits;

    SearchHits searchHits = searchResponse.getHits();
    for (int i = 0; i < searchHits.getHits().length; i++) {
    String str = searchHits.getHits()[i].getSourceAsString();
        }
    }

     5.删除数据

            DeleteRequest deleteRequest = new DeleteRequest();
            deleteRequest.index(index).type(type).id(id);
    
            try {
                highLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
            } catch (Exception ex) {
                log.error("删除ES中的数据异常", ex);
            }

    参考:

    https://www.cnblogs.com/javallh/p/13790486.html

    https://www.cnblogs.com/dreamroute/p/8484457.html

    https://www.cnblogs.com/zwt1990/p/7737747.html

    http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

    https://www.cnblogs.com/yixinjishu/p/11057933.html

  • 相关阅读:
    如何运行vue项目
    java string型时间转long
    龙果学院dubbo
    百度网盘
    百度网盘资源搜索
    charles安装
    charles抓包
    lr报告分析
    一些缩写意思
    带有事务的进入首页、登录、登出脚本
  • 原文地址:https://www.cnblogs.com/june0816/p/11072471.html
Copyright © 2011-2022 走看看