Doc地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.10/index.html
1.pom.xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version>
</dependency>
2.Test.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
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.client.Node;
import org.elasticsearch.client.NodeSelector;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.GetSourceRequest;
import org.elasticsearch.client.core.GetSourceResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
public class Test {
public static void main(String[] args) throws IOException {
ArrayList<HttpHost> hostList = new ArrayList<>();
List<Node> nodes = new ArrayList<>();
hostList.add(new HttpHost("127.0.0.1", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(hostList.get(0)));
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field("title", "test");
builder.timeField("date", new Date());
builder.field("content","Biden didn't mention his predecessor by name, "
+ "but especially in the early moments of his speech, "
+ "it was very clear that the current President lays much of the blame for "
+ "the country's struggles with the coronavirus "
+ "pandemic at the feet of the last President."
+ "A year ago, we were hit with a virus that was met with silence and spread unchecked, "
+ "denials for days, weeks, then months, "
+ "Biden said at one point. "
+ "That led to more deaths, more infections, more stress and more loneliness."
+ "At another point, Biden pulled out his mask "
+ "and expressed amazement that it had been turned into some sort of political statement.") ;
}
builder.endObject();
IndexRequest request = new IndexRequest("cn-news")
.id("3").source(builder);
/*IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response);
client.close();*/
searchSourceWithFulltextMatch(client);
}
/**
* @Title: searchSourceWithDocId
* @Description: 通过DocId查询
* @author: 徐蜀黍
* @date: 2021年3月12日 下午4:03:20
* @param client
*/
public static void searchSourceWithDocId(RestHighLevelClient client) {
try {
GetSourceRequest getSourceRequest = new GetSourceRequest("cn-news", "1");
GetSourceResponse response = client.getSource(getSourceRequest, RequestOptions.DEFAULT);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Title: searchAllSourceWithIndexs
* @Description:查询指定index的全部数据
* @author: 徐蜀黍
* @date: 2021年3月12日 下午4:03:46
* @param client
*/
public static void searchAllSourceWithIndexs(RestHighLevelClient client) {
try {
SearchRequest searchRequest = new SearchRequest("cn-news");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Title: searchSourceWithCondition
* @Description:按条件分页查询
* @author: 徐蜀黍
* @date: 2021年3月12日 下午4:04:36
* @param client
* @param condition
*/
public static void searchSourceWithCondition(RestHighLevelClient client, String condition) {
try {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(Objects.isNull(condition) ? QueryBuilders.matchAllQuery()
: QueryBuilders.queryStringQuery(condition));
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout();
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cn-news");
searchRequest.source(sourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Title: searchSourceWithFulltextMatch
* @Description:全文检索
* @author: 徐蜀黍
* @date: 2021年3月12日 下午4:42:58
* @param client
*/
public static void searchSourceWithFulltextMatch(RestHighLevelClient client) {
try {
SearchRequest searchRequest = new SearchRequest("cn-news");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 设置高亮HighlightBuilder
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
HighlightBuilder.Field highlightContent = new HighlightBuilder.Field("content");
highlightTitle.highlighterType("unified");
highlightBuilder.field(highlightTitle);
highlightBuilder.field(highlightContent);
// 设置查询QueryBuilders
searchSourceBuilder.query(QueryBuilders.multiMatchQuery("test", "title", "content"))
.sort(new ScoreSortBuilder().order(SortOrder.DESC))
.highlighter(highlightBuilder)
.from(0)
.size(10);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}