注意,索引的字段mapping是如下类型,如果index是FALSE的话,会导致条件查询失败:
![](https://i-blog.csdnimg.cn/blog_migrate/f83bbd7baf4b941a1b33bc2c0cad4926.png)
先引入pom依赖:
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch依赖2.x的log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
创建User类:
public class User {
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
然后在名为user的索引里,插入多条数据,在main方法里进行测试:
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Insert_Batch {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 批量插入数据
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age",30,"sex","男"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age",30,"sex","女"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age",40,"sex","男"));
request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu1", "age",40,"sex","女"));
request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu2", "age",50,"sex","男"));
request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu3", "age",50,"sex","男"));
request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu44", "age",60,"sex","男"));
request.add(new IndexRequest().index("user").id("1008").source(XContentType.JSON, "name", "wangwu555", "age",60,"sex","男"));
request.add(new IndexRequest().index("user").id("1009").source(XContentType.JSON, "name", "wangwu66666", "age",60,"sex","男"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println("====>"+response.getTook());
System.out.println("====>"+response.getItems());
esClient.close();
}
}
然后进行测试:
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class ESTest_Doc_Query {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 1. 查询索引中全部的数据
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
//
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 2. 条件查询 : termQuery
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 40)));
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 3. 分页查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
// // (当前页码-1)*每页显示数据条数
// builder.from(2);
// builder.size(2);
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 4. 查询排序
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
// //
// builder.sort("age", SortOrder.DESC);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 5. 过滤字段
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
// //
// String[] excludes = {"age"};
// String[] includes = {};
// builder.fetchSource(includes, excludes);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 6. 组合查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder();
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//
// //boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
// //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));
// //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));
// boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
// boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
//
// builder.query(boolQueryBuilder);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 7. 范围查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder();
// RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//
// rangeQuery.gte(30);
// rangeQuery.lt(50);
//
// builder.query(rangeQuery);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 8. 模糊查询
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
//这里的Fuzziness.TWO是查询结果的字符串长度偏差,可以设置成Fuzziness.ONE看一下效果
builder.query(QueryBuilders.fuzzyQuery("name", "wangwu").fuzziness(Fuzziness.TWO));
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
System.out.println(response.getTook());
for ( SearchHit hit : hits ) {
System.out.println(hit.getSourceAsString());
}
// // 9. 高亮查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder();
// TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
//
// HighlightBuilder highlightBuilder = new HighlightBuilder();
// highlightBuilder.preTags("<font color='red'>");
// highlightBuilder.postTags("</font>");
// highlightBuilder.field("name");
//
// builder.highlighter(highlightBuilder);
// builder.query(termsQueryBuilder);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 10. 聚合查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder();
//
// AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
// builder.aggregation(aggregationBuilder);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 11. 分组查询
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// SearchSourceBuilder builder = new SearchSourceBuilder();
//
// AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
// builder.aggregation(aggregationBuilder);
//
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// SearchHits hits = response.getHits();
//
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
//
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
esClient.close();
}
}