import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.util.NamedValue;
import org.example.Main;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.ExtendedRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @Author 陈康语
* @Date 2024/9/16 14:21
*/
@SpringBootTest(classes = Main.class)
public class Tes1 {
@Autowired
private ElasticsearchClient elasticsearchClient;
@Test
public void e1() throws IOException {
SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info")
.query(bl -> bl.bool(mt -> mt
.must(mh -> mh.match(q -> q.field("title").query("三国")))
.must(mh -> mh.match(q -> q.field("user_name").query("张涛")))
.filter(re->re.range(q->q.field("price").gt(JsonData.of(50))))
));
SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
List<Hit<Object>> hits = search.hits().hits();
for (Hit<Object> hit : hits){
System.out.println(hit.source());
}
}
/**
* 查询
* 1.作者是何林or张之盐
* 2.商品的价格在10-100之间
* 3.分页查询第0页,每页3条数据
* 4.根据价格进行排序
* 5.只返回title,price,user_name
* @throws IOException
*/
@Test
public void e2() throws IOException {
SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info")
.query(bl->bl.bool(sd->sd
.should(mh->mh.match(q->q.field("user_name").query("何林")))
.should(mh->mh.matchPhrase(q->q.field("user_name").query("张之盐")))
.filter(rg->rg.range(pe->pe.field("price").gt(JsonData.of(10)).lt(JsonData.of(100))))
))
.from(0)
.size(3)
.sort(s->s.field(q->q.field("price").order(SortOrder.Desc)))
.source(f->f.filter(v->v.includes("title","price","user_name")));
SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
List<Hit<Object>> hits = search.hits().hits();
for (Hit<Object> hit : hits){
System.out.println(hit.source());
}
}
/**
* 根据category 进行分组查询
* @throws IOException
*/
@Test
public void e3() throws IOException {
SearchRequest.Builder query = new SearchRequest.Builder().index("sku_info");
query.size(0)
.aggregations("category_group",
ts->ts.terms(fd->fd.field("category").size(2).order(NamedValue.of("_key", SortOrder.Desc))).aggregations("top_category", t->t.topHits(q->q.size(2)))
);
SearchResponse<Object> search = elasticsearchClient.search(query.build(), Object.class);
//解构结果
Map<String, Aggregate> aggregations = search.aggregations();
Aggregate buckets = aggregations.get("category_group");
buckets.lterms().buckets().array().forEach(b->{
System.out.println(b.key()+"--"+b.docCount());
b.aggregations().get("top_category").topHits().hits().hits().forEach(h->System.out.println(h.source()));
});
}
}
基础使用es
最新推荐文章于 2024-11-01 10:27:08 发布