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操作ES的方式,建议使用rest方式

1.TransportClient,通过TCP方式访问ES,只支持java,以后可能废除,不建议使用

a.引入依赖

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.7.1</version>
        </dependency>

b.创建client

// 设置集群的名字
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", false)
                .put("request.headers.Authorization", basicAuthHeaderValue(userName, password))
                .build();
        // 创建集群client并添加集群节点地址
        PreBuiltTransportClient c = new PreBuiltTransportClient(settings);
        c.addTransportAddress(new TransportAddress(InetAddress.getByName(host), Integer.parseInt(port)));

c.使用client增删改查

 

2.JestClient

a.引入依赖

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
        </dependency>

b.创建client

https://blog.csdn.net/weixin_42685328/article/details/111408562

c.使用client增删改查

 

3.RestClient,通过HTTP方式访问ES,没有语言限制,和transport不能同时使用

low-level-client

https://blog.csdn.net/weixin_33831673/article/details/91447794

 

4.RestHighLevelClient,推荐使用

a.引入jar包

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

b.创建client

@Configuration
public class EsConfig {
    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);
    }

}

c.写数据

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);

d.读数据 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();
    }
}

e.删除数据

        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.index(index).type(type).id(id);

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

 

5.Spring-data-elasticsearch

https://blog.csdn.net/qq_44657532/article/details/123622136

 

6.bboss框架

https://esdoc.bbossgroups.com/#/quickstart

 

 

 

参考:

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

https://blog.csdn.net/qq_41937849/article/details/87368585

 

posted @ 2021-11-22 13:48  牧云文仔  阅读(7418)  评论(0编辑  收藏  举报