创建客户端
public static RestHighLevelClient getClient() {
String elasticsearchHosts = "10.1.24.223:9200,10.1.24.224:9200,10.1.24.225:9200";
String[] hosts = elasticsearchHosts.split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < hosts.length; i++) {
String[] hostAndPort = hosts[i].split(":");
String host = hostAndPort[0];
int port = Integer.parseInt(hostAndPort[1]);
httpHosts[i] = new HttpHost(host, port, "http");
}
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(httpHosts));
return client;
}
查询
需求
实现如下查询:
GET /disease/_search
{
"_source": {
"includes": [ "disease_*", "info" ],
"excludes": [ "" ]
},
"query": {
"bool": {
"filter": [
{
"range": {
"high_age": {
"gte": 10
}
}
},
{
"range": {
"low_age": {
"lte": 10
}
}
}
],
"should": [
{
"match": {
"all_info": """ 恶心["空腹"] 头晕 次数["以上都不是"] 呕吐["清晨"] 呕吐["呕吐后干呕不止"] 呕吐["特定情况下会呕吐"] 腹股沟肿物["鸡蛋大小"] 腹痛["吃东西时"] 腹痛["上腹"] 腹痛["绞痛"] 腹痛["特定情况下会疼痛"] 腹痛["暴饮暴食"] 大便["鲜血便"] 腹泻频繁["特定情况下会腹泻"] 腹泻时间["吃东西时"]"""
}
}
],
"must_not": [
{
"match": {
"symptoms": ""
}
},
{
"match_phrase": {
"gender": "0"
}
},
{
"match_phrase": {
"pregnancy_flag": "1"
}
}
],
"must": [
{
"match": {
"symptoms": " 恶心 头晕 恶心与呕吐 发热 腹股沟淋巴结肿大 腹痛 打嗝 稀便"
}
}
]
}
}
}
对应java代码
@Test
public void test() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("disease");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 分页所需参数
sourceBuilder.size(3);
sourceBuilder.from(0);
// 过滤字段
// 第一个参数是要查询的字段,第二个参数是要过滤掉的字段
sourceBuilder.fetchSource(new String[]{"disease_*", "info"}, new String[]{});
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.filter(QueryBuilders.rangeQuery("low_age").gte(10));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("high_age").gte(10));
boolQueryBuilder.should(QueryBuilders.matchQuery("all_info", "恶心[\"空腹\"]"));
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("symptoms", "咳嗽"));
boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery("gender", 0));
boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery("pregnancy_flag", 1));
boolQueryBuilder.must(QueryBuilders.matchQuery("symptoms", "恶心 头晕 恶心与呕吐 发热 腹股沟淋巴结肿大 腹痛 打嗝 稀便"));
sourceBuilder.query(boolQueryBuilder);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
System.out.println(hits.length);
for (int i = 0; i < hits.length; i++) {
SearchHit hit = hits[i];
System.out.println(hit.getScore());
System.out.println(hit.getSourceAsString());
}
}
插入
@Test
public void addDoc() throws IOException {
IndexRequest request = new IndexRequest("index_member1");
request.id("1");
JSONObject params = new JSONObject();
params.put("username", "zs");
params.put("password", "bicon@123");
params.put("birthday", 1588147160000L);
params.put("height", 175);
params.put("gender", 0);
request.source(params, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
int status = response.status().getStatus();
System.out.println(response);
System.out.println(status);
}
更新
更新的时候对于没有传入的字段,不做处理。如果想把字段置为null
,则需要传入字段名=null
@Test
public void updateIndex() throws IOException {
UpdateRequest request = new UpdateRequest();
request.index("index_member1");
request.id("1");
JSONObject params = new JSONObject();
params.put("username", "ls");
params.put("password", "bicon@1234");
params.put("birthday", null);
request.doc(params, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response);
}