前言:
简单查询请参考:https://mp.csdn.net/mp_blog/creation/editor/141529910
1.match字段查询
@Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// match查询
SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
.query(q -> q.match(
m -> m.field("name").query("铁锤")
)), Produce.class);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
2.matchAll查询
@Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
//matchAll查询
SearchResponse<Produce> search = elasticsearchClient.search(s->s
.index("new_product")
.query(q->q
.matchAll(m->m)),Produce.class);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
3.matchPhrase短句查询
@Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
//matchPhrase短句查询
SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
.query(q -> q.matchPhrase(
m->m.field("name").query("漂亮的大铁锤")
)), Produce.class);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
4.term 不分词查询
@Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
//term查询
SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
.query(q -> q.term(t -> t.field("stock").value("666"))), Produce.class);
System.out.printf("------");
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
5.多条件查询
使用bool关键字配合must,should,must_not
must:所有条件必须同时成立
must_not:所有条件必须同时不成立
should:所有条件中成立一个即可
@Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
SearchResponse<Produce> search = elasticsearchClient.search(s -> s.index("new_product")
.query(q -> q
.bool(builder -> builder
.must(m -> m
.term(t -> t
.field("name").value("漂亮的大铁锤")))
.must(m -> m
.term(t -> t
.field("intro").value("大铁锤"))))), Produce.class
);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
6.multiMatch多字段查询
Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// multiMatch多字段查询
SearchResponse<Produce> new_product = elasticsearchClient.search(s -> s.index("new_product")
.query(q->q
.multiMatch(m->m
.query("铁锤").fields("name","intro"))), Produce.class);
new_product.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
7.分页查询
from为页码
Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// 分页查询
SearchResponse<Produce> new_product = elasticsearchClient.search(s -> s
.index("new_product").query(q -> q
.matchAll(v -> v))
.size(10)
.from(0), Produce.class);
new_product.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
8. 排序
Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
SearchResponse<Produce> search = elasticsearchClient.search(s -> s
.index("new_product")
.query(q -> q.matchAll(v -> v))
.sort(so -> so.field(f -> f.field("name").order(SortOrder.Asc))), Produce.class);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
9.指定查询字段查询
Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// 指定查询字段查询
SearchResponse<Produce> search = elasticsearchClient.search(s -> s
.index("new_product")
.query(q -> q.matchAll(v -> v))
.source(sou -> sou.filter(f -> f.includes("name", "intro"))), Produce.class);
search.hits().hits().forEach(produceHit -> {
System.out.printf(String.valueOf(produceHit.source()));
});
}
10.聚合查询
Test
public void contextLoads() throws IOException {
ElasticsearchClient elasticsearchClient = elasticSearchConfig.esRestClient();
// 指定查询字段查询
SearchResponse<Object> search = elasticsearchClient.search(s -> s
.index("new_product")
.query(q -> q.matchAll(v -> v))
.aggregations("stockAgg", a -> a.terms(t -> t.field("stock"))), Object.class);
Aggregate stockAgg = search.aggregations().get("stockAgg");
List<LongTermsBucket> array = stockAgg.lterms().buckets().array();
}
持续更新中》〉》〉》