Elasticsearch官方建议使用新版的Java Api Client替代原有的Rest客户端,这篇文章会简单讲解
新版api的使用。
The Elasticsearch Java API Client is an entirely new client library that has no relation to the older High Level Rest Client (HLRC). This was a deliberate choice to provide a library that is independent from the Elasticsearch server code and that provides a very consistent and easier to use API for all Elasticsearch features.
首先引入依赖
<elastic.client.version>7.17.5</elastic.client.version>
<jackson.databind>2.13.2</jackson.databind>
<jakarta.json>2.0.1</jakarta.json>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elastic.client.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.databind}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.databind}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>${jakarta.json}</version>
</dependency>
创建elasticsearch客户端文章来源:https://www.toymoban.com/news/detail-510204.html
package com.pic.framework.config;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
/**
* @author 91989
* @title: ElasticSearchConfig
* @projectName xy-reader
* @description: ES配置类
* @date 2022/6/15 15:26
*/
@Configuration
public class ElasticSearchConfig {
//注入IOC容器
@Bean
public ElasticsearchClient elasticsearchClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "111111"));
RestClient client = RestClient.builder(new HttpHost("localhost", 9200, "http"))
.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
httpAsyncClientBuilder.disableAuthCaching();
return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}).build();
ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
}
}
相关CRUD操作文章来源地址https://www.toymoban.com/news/detail-510204.html
package com.reader.web.controller.es;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.mapping.*;
import co.elastic.clients.elasticsearch._types.query_dsl.*;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.HighlightBase;
import co.elastic.clients.elasticsearch.core.search.HighlightField;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.IndexSettings;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.reader.common.core.domain.AjaxResult;
import com.reader.common.core.domain.entity.SysUser;
import com.reader.framework.config.ElasticSearchConfig;
import javafx.scene.input.PickResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.swing.text.Highlighter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 91989
* @title: EsTestController
* @projectName xy-reader
* @description: TODO
* @date 2022/7/1 16:42
*/
@SuppressWarnings("rawtypes")
@RestController
@RequestMapping("/es")
public class EsTestController {
@Autowired
private ElasticsearchClient client;
@PostMapping("/createIndex")
public AjaxResult createIndex(String indexName) throws IOException {
//定义索引的别名
String KEYWORD = "";
String indexAliasName = indexName + "_alias";
Map<String, Property> propertyMap = new HashMap<>();
//构建索引类型
propertyMap.put("content_id", new Property(new IntegerNumberProperty.Builder().index(true).build()));
propertyMap.put("content", new Property(new TextProperty.Builder().index(true).analyzer("ik_max_word").build()));
propertyMap.put("content_en", new Property(new TextProperty.Builder().index(true).analyzer("standard").build()));
propertyMap.put("create_time", new Property(new DateProperty.Builder().index(true).build()));
//构建设置
TypeMapping mapping = new TypeMapping.Builder().properties(propertyMap).build();
IndexSettings indexSettings = new IndexSettings.Builder().numberOfShards(String.valueOf(1)).numberOfReplicas(String.valueOf(0)).build();
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
.index("ik_test_index")
.mappings(mapping)
.settings(indexSettings)
.build();
//执行创建索引操作并返回结果
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest);
return AjaxResult.success();
}
/**
* * @description: 插入文档
* * @author Lin
* * @date 2022/7/5 17:26
*/
@PostMapping("/insertDoc")
public AjaxResult insertDoc(String doc, String docEn) throws IOException {
String indexName = "ik_test_index";
Map<String, Object> paramMap = new HashMap<>();
int[] randomInt = NumberUtil.generateRandomNumber(1, 10000, 1);
paramMap.put("content_id", randomInt[0]);
paramMap.put("content", doc);
paramMap.put("content_en", docEn);
paramMap.put("create_time", DateUtil.date());
//插入文档
IndexResponse response = client.index(i -> i.index(indexName).document(paramMap));
return AjaxResult.success(response);
}
/**
* * @description: 基于索引数据的多维度查询
* * @author Lin
* * @date 2022/7/7 9:56
*/
@GetMapping("/getEs")
public AjaxResult getEs(String keyword) throws IOException {
String str = "li";
//分词查询构造器
MatchQuery matchQuery = QueryBuilders.match().field("content_en").query(keyword).build();
//布尔查询构造器
BoolQuery.Builder queryBuilders = QueryBuilders.bool();
queryBuilders.must(matchQuery._toQuery());
//模糊匹配查询构造器 类似like查询
WildcardQuery.Builder wildBuilder = QueryBuilders.wildcard();
str = StrUtil.addSuffixIfNot(str, "*");
WildcardQuery wildcardQuery = wildBuilder.field("name").wildcard(str).build();
//精确匹配查询构造器
TermQuery.Builder termBuilder = QueryBuilders.term();
TermQuery termQuery = termBuilder.field("content_id").value("name").build();
//布尔查询增加and条件
queryBuilders.must(termQuery._toQuery());
//构建出布尔查询
BoolQuery boolQuery = queryBuilders.build();
//高亮查询
HighlightField.Builder highlightFieldBuilder = new HighlightField.Builder();
HighlightField highlightField = highlightFieldBuilder.build();
//构建高亮字段
Highlight.Builder highlightBuilder = new Highlight.Builder();
Highlight highlight = highlightBuilder.fields("content_en", highlightField).build();
SearchRequest searchRequest = new SearchRequest.Builder().query(matchQuery._toQuery()).highlight(highlight).index("ik_test_index").build();
/*SearchRequest searchRequest = new SearchRequest.Builder().index("new_index").build();*/
/* SearchResponse search = client.search(s -> s.index("new_index").
query(q -> q.match(m -> m.field("name").query(r->r.stringValue("li")))),
Map.class);*/
SearchResponse searchResponse = client.search(searchRequest, Map.class);
//命中list
List<Hit> list = searchResponse.hits().hits();
//处理结果list
List<Map<String,Object>> resList = new ArrayList<>();
for (Hit hit : list) {
Map<String,Object> resMap = new HashMap<>();
Map highLight = hit.highlight();
Double score = hit.score();
Map map = (Map) hit.source();
resMap.put("highLight",highLight);
resMap.put("score",score);
resMap.put("source",map);
resList.add(resMap);
}
return AjaxResult.success(resList);
}
}
到了这里,关于Elasticsearch 7.17 Java Client Api的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!