RestHighLevelClient操作
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.15.2</version>
</dependency>
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RestClientConfig {
@Value("${es.iphosts:localhost:9200}")
private String ipHosts;
@Value("${es.password:null}")
private String password;
@Value("${es.username:null}")
private String username;
@Bean(destroyMethod = "close")
public RestHighLevelClient restHighLevelClient() {
List<HttpHost> hosts = new ArrayList<>();
String[] iphostArray = ipHosts.split(";");
for (int i = 0; i < iphostArray.length; i++) {
if (StringUtils.isEmpty(iphostArray[i])) {
continue;
}
hosts.add(new HttpHost(iphostArray[i].split(":")[0],
Integer.valueOf(iphostArray[i].split(":")[1]),"http"));
}
Header[] headers = new BasicHeader[2];
headers[0] = new BasicHeader("username",username);
headers[1] = new BasicHeader("password",password);
RestClientBuilder builder = RestClient.builder(hosts.stream().toArray(HttpHost[]::new))
.setDefaultHeaders(headers)
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
return builder.setConnectTimeout(5000 * 1000)
.setSocketTimeout(6000 * 1000);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
import com.alibaba.fastjson.JSONObject;
import com.example.demo.es.entity.Product;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedDoubleTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.ParsedSum;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class EsTest {
@Autowired
private RestHighLevelClient client;
@Test
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("products");
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
request.mapping(
"{\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"title\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"double\"\n" +
" },\n" +
" \"created_at\": {\n" +
" \"type\": \"date\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\"\n" +
" },\n" +
" \"brand\": {\n" +
" \"type\": \"keyword\"\n" +
" }\n" +
" }\n" +
" }",
XContentType.JSON);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isAcknowledged());
}
@Test
public void insertDoc() throws IOException {
IndexRequest request=new IndexRequest("products");
Product product=new Product();
product.setId(1);
product.setTitle("小米手机");
product.setPrice(1999.9d);
product.setDescription("小米手机正好");
request.id(product.getId().toString());
request.source(JSONObject.toJSONString(product), XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.getResult());
}
@Test
public void updateDocById() throws IOException {
UpdateRequest request=new UpdateRequest();
request.index("products").id("1");
Product product=new Product();
product.setDescription("小米发烧为生");
request.doc(JSONObject.toJSONString(product), XContentType.JSON);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.getResult());
}
@Test
public void delDocById() throws IOException {
DeleteRequest deleteRequest=new DeleteRequest();
deleteRequest.index("products").id("1");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.getResult());
}
@Test
public void getDocById() throws IOException {
GetRequest getRequest=new GetRequest();
getRequest.index("products").id("1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("查出的数据为:"+ getResponse.getSourceAsString());
}
@Test
public void matchAllTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
query(queryBuilder);
}
public void query(QueryBuilder queryBuilder )throws IOException{
SearchRequest searchRequest=new SearchRequest();
searchRequest.indices("products");
searchRequest.source(new SearchSourceBuilder().query(queryBuilder));
System.out.println("查询DSL为:"+searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("返回结果为:"+searchRes.toString());
System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
SearchHit[] hits = searchRes.getHits().getHits();
for (SearchHit hit: hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
client.close();
}
@Test
public void termTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.termQuery("description","小米");
query(queryBuilder);
}
@Test
public void rangeTest() throws IOException {
RangeQueryBuilder queryBuilder=QueryBuilders.rangeQuery("price");
queryBuilder.gte(100);
queryBuilder.lt(300);
query(queryBuilder);
}
@Test
public void prefixTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.prefixQuery("description","小米");
query(queryBuilder);
}
@Test
public void wildcardTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("title","*红*");
query(queryBuilder);
}
@Test
public void fuzzyTest() throws IOException {
FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("title", "红米大机").fuzziness(Fuzziness.TWO);
query(queryBuilder);
}
@Test
public void idsTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1","3");
query(queryBuilder);
}
@Test
public void multiMatchTest() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("真棒").field("title").field("description");
query(queryBuilder);
}
@Test
public void pageTest() throws IOException {
SearchRequest searchRequest=new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.from(0).size(2);
query.sort("price", SortOrder.DESC);
String[] include={};
String[] exclude={"created_at"};
query.fetchSource(include,exclude);
searchRequest.source(query);
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
SearchHit[] hits = searchRes.getHits().getHits();
for (SearchHit hit: hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
client.close();
}
@Test
public void highlightTest() throws IOException {
SearchRequest searchRequest=new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("description","苹果"));
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>");
highlightBuilder.postTags("</font>");
highlightBuilder.field("title").field("description");
highlightBuilder.requireFieldMatch(false);
query.highlighter(highlightBuilder);
searchRequest.source(query);
System.out.println("查询DSL为:"+searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("返回结果为:"+searchRes.toString());
System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
SearchHit[] hits = searchRes.getHits().getHits();
for (SearchHit hit: hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if(highlightFields.containsKey("description")){
System.out.println("description高亮的结果:"+highlightFields.get("description").fragments()[0]);
}
if(highlightFields.containsKey("title")){
System.out.println("title高亮的结果:"+highlightFields.get("title").fragments()[0]);
}
}
client.close();
}
@Test
public void filterTest() throws IOException {
SearchRequest searchRequest=new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.termQuery("description","手机"));
builder.postFilter(QueryBuilders.existsQuery("title"));
searchRequest.source(builder);
System.out.println("查询DSL为:"+searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("返回结果为:"+searchRes.toString());
System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
SearchHit[] hits = searchRes.getHits().getHits();
for (SearchHit hit: hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
client.close();
}
@Test
public void composeTest() throws IOException {
SearchRequest searchRequest=new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder=QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("title","红米手机"));
boolQueryBuilder.should(QueryBuilders.rangeQuery("price").gt(100).lt(200));
builder.query(boolQueryBuilder);
searchRequest.source(builder);
System.out.println("查询DSL为:"+searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("返回结果为:"+searchRes.toString());
System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
SearchHit[] hits = searchRes.getHits().getHits();
for (SearchHit hit: hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
client.close();
}
@Test
public void aggsTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("priceGroup").field("price");
builder.aggregation(aggregationBuilder);
searchRequest.source(builder);
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchRes.getHits();
for (SearchHit hit : hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
Aggregations aggregations = searchRes.getAggregations();
ParsedDoubleTerms parsedDoubleTerms = aggregations.get("priceGroup");
List<? extends Terms.Bucket> buckets = parsedDoubleTerms.getBuckets();
for (Terms.Bucket bucket:buckets){
System.out.println("key:为"+bucket.getKeyAsString()+",count数量为:"+bucket.getDocCount());
}
client.close();
}
@Test
public void aggsOperateTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.sum("priceSum").field("price");
builder.aggregation(aggregationBuilder);
searchRequest.source(builder);
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchRes.getHits();
for (SearchHit hit : hits) {
System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
}
Aggregations aggregations = searchRes.getAggregations();
ParsedSum parsedSum = aggregations.get("priceSum");
double sumValue = parsedSum.getValue();
System.out.println("求和值为:"+sumValue);
client.close();
}
@Test
public void collapseTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.matchQuery("brand", "vivo");
builder.query(queryBuilder);
CollapseBuilder collapseBuilder = new CollapseBuilder("price");
List<InnerHitBuilder> innerHits = new ArrayList<>();
InnerHitBuilder innerHitBuilder = new InnerHitBuilder();
innerHitBuilder.setName("r_date");
innerHitBuilder.addSort(SortBuilders.fieldSort("created_at").order(SortOrder.DESC));
innerHitBuilder.setSize(10);
innerHitBuilder.setInnerCollapse(new CollapseBuilder("title"));
innerHits.add(innerHitBuilder);
collapseBuilder.setInnerHits(innerHits);
builder.collapse(collapseBuilder);
searchRequest.source(builder);
System.out.println("查询DSL为:" + searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchRes.getHits();
for (SearchHit hit : hits) {
System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
Map<String, SearchHits> innerHitsRes = hit.getInnerHits();
for (Map.Entry<String, SearchHits> map : innerHitsRes.entrySet()) {
String fieldKey = map.getKey();
SearchHits innerSearchHits = map.getValue();
SearchHit[] innerHitRes = innerSearchHits.getHits();
for (SearchHit h : innerHitRes) {
System.out.println("collapse field: " + fieldKey + ",id:为" + h.getId() + ",数据为:" + h.getSourceAsString());
}
}
}
client.close();
}
@Test
public void aggsTopHitsTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.topHits("price_top").sort(SortBuilders.fieldSort("price").order(SortOrder.DESC)).size(2);
builder.aggregation(aggregationBuilder);
builder.size(0);
searchRequest.source(builder);
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchRes.getHits();
for (SearchHit hit : hits) {
System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
}
Aggregations aggregations = searchRes.getAggregations();
TopHits topHits = aggregations.get("price_top");
SearchHits hitsRes = topHits.getHits();
for (SearchHit hit : hitsRes.getHits()) {
System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
}
client.close();
}
@Test
public void aggsPercentileRanksTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("products");
SearchSourceBuilder builder = new SearchSourceBuilder();
double[] rankVal={200d,300d,500d};
PercentileRanksAggregationBuilder aggregationBuilder = AggregationBuilders.percentileRanks("price_ranks",rankVal);
aggregationBuilder.field("price");
builder.aggregation(aggregationBuilder);
builder.size(0);
searchRequest.source(builder);
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchRes.getHits();
for (SearchHit hit : hits) {
System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
}
Aggregations aggregations = searchRes.getAggregations();
PercentileRanks rercentileRanks = aggregations.get("price_ranks");
for (Percentile entry : rercentileRanks) {
double percent = entry.getPercent();
double value = entry.getValue();
System.out.println("value:"+value+"percent:"+percent);
}
client.close();
}
@Test
public void autoCompleteTest() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("stars");
SearchSourceBuilder builder = new SearchSourceBuilder();
CompletionSuggestionBuilder completionSuggestionBuilder=new CompletionSuggestionBuilder("name").skipDuplicates(true).size(10);
completionSuggestionBuilder.prefix("liu");
SuggestBuilder suggestBuilder=new SuggestBuilder();
suggestBuilder.addSuggestion("star_name_suggest",completionSuggestionBuilder);
builder.suggest(suggestBuilder);
builder.size(0);
builder.fetchSource(false);
searchRequest.source(builder);
System.out.println("查询DSL为:" + searchRequest.source().toString());
SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
CompletionSuggestion
suggest = searchRes.getSuggest().getSuggestion("star_name_suggest");
ArrayList<String> list=new ArrayList<>();
suggest.forEach(c->c.getOptions().forEach(f->
list.add( f.getText().toString())
));
list.forEach(System.out::println);
client.close();
}
}
有一些写法可以参考 https://www.kgraph.cn/218.html
|