elasticsearch8.0部署和java调用

返回
Author Avatar
钢翼
2022-03-02
编程
59

elasticsearch8.0

一、部署

从官网下载8.0 https://www.elastic.co/cn/downloads/elasticsearch

直接解压。

修改config/jvm.options,根据你服务器性能配置jvm,如下

-Xms2g
-Xmx2g

修改config/elasticsearch.yml

cluster.name: my-application
node.name: node-1
path.data: E:/elasticsearch-8.0.0-windows-x86_64/elasticsearch-8.0.0/data
path.logs:  E:/elasticsearch-8.0.0-windows-x86_64/elasticsearch-8.0.0/logs
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
#不使用geoip
ingest.geoip.downloader.enabled: false
#不检查磁盘容量,默认如果磁盘占用超过95%,索引会变只读,不可修改。
cluster.routing.allocation.disk.threshold_enabled: false

运行bin/elasticsearch.bat(windows),第一次启动后在config/elasticsearch.yml生成安全相关配置。 继续修改config/elasticsearch.yml,将以下两个配置改为false

xpack.security.enabled: false

xpack.security.enrollment.enabled: false

重新运行bin/elasticsearch.bat(windows)

在浏览器访问 http://localhost:9200 检查服务是否正常

检查索引状态 http://localhost:9200/_cat/indices?v

检查插件 http://localhost:9200/_cat/plugins?v

安装中文分词器

默认分词器只会将中文分成1个个字,需要安装中文分词器并设置索引才能更好的查询中文。 在 https://github.com/medcl/elasticsearch-analysis-ik/releases 上找到对应版本的下载。 在elasticsearch应用程序的plugins目录下新建 ik目录,并将下载的包解压进去。

建立索引时,对于中文字段,一般插入索引使用ik_max_word,查询索引使用 ik_smart

二、java调用

官方文档 https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.0/index.html

吐槽下官方文档,直接引用会报找不到类,而且只给了查询demo,插入删除都没demo

pom.xml添加引用

  <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>jakarta.json</groupId>
                    <artifactId>jakarta.json-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
            <version>2.0.1</version>
        </dependency>

构造一个ElasticsearchClient,es的9200端口是http协议,9300端口是tcp协议

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);

插入文档

User user = new User("1234","张三");
 IndexResponse index = client.index(s ->
                        s.index("user")
                                .id(user.getId())
                                .document(user)
                );

批量插入文档

List<User> users  = new ArrayList<>();
users.add(new User("1234","张三"));
List<BulkOperation> bulkOperations = new ArrayList<>();
users.forEach(user -> { 
	bulkOperations.add(BulkOperation.of(b -> b.index(c -> c.id(user.getId()).document(user))));
});
BulkResponse bulk = client.bulk(x -> x.index("user").operations(bulkOperations));

更新文档(换成插入也可以)

User user = new User("123456","张三");
 UpdateResponse<User> update = client.update(
                        s -> s.index("user").id(user.getId()).doc(user), User.class);

删除文档

	String id = "1234";
    DeleteResponse delete = client.delete(s -> s.index("user")
                        .id(id));

查询

SearchResponse<Map> search = client.search(s -> s
	.index("user")
	.query(q -> q
	.match(t -> t
	.field("name")
	.query(v -> v.stringValue("张三"))
	)),
	User.class);
List<User> list = new ArrayList<>();
for (Hit<User> hit : search.hits().hits()) {
list.add(hit.source());
}