0、数据准备:
1、TermQueryBuilder查询(查询name为tom的文档)
public void search() {
SearchRequestBuilder search = client.prepareSearch("book");
search.setTypes("novel");
search.setSearchType(SearchType.QUERY_THEN_FETCH);
//查询name为tom的文档
QueryBuilder term = QueryBuilders.termQuery("name", "tom");
search.setQuery(term);
SearchResponse response = search.get();
for(SearchHit hit : response.getHits().getHits()) {
System.out.println(hit.getSource() + "---->" + hit.getId());
}
}
返回结果:
{name=Tom, count=5000, title=标题9991, age=13, height=157}---->9991
{name=Tom, count=15500, title=测试, age=12, height=169}---->1
2、TermsQueryBuilder查询(查询name为同名和alice的文档)
public void search() {
SearchRequestBuilder search = client.prepareSearch("book");
search.setTypes("novel");
search.setSearchType(SearchType.QUERY_THEN_FETCH);
//查询name为tom的文档
//QueryBuilder term = QueryBuilders.termQuery("name", "tom");
//查询name为tom和alice的文档
QueryBuilder terms = QueryBuilders.termsQuery("name", "tom","alice");
search.setQuery(terms);
SearchResponse response = search.get();
for(SearchHit hit : response.getHits().getHits()) {
System.out.println(hit.getSource() + "---->" + hit.getId());
}
}
返回结果:
{name=Alice, count=4000, title=标题9999, age=12, height=156}---->9999
{name=Tom, count=5000, title=标题9991, age=13, height=157}---->9991
{name=Tom, count=15500, title=测试, age=12, height=169}---->1
3、RangeQueryBuilder范围查询(查询年龄>=12 and <=40)
RangeQueryBuilder query = QueryBuilders.rangeQuery("age");
query.gte(12).lt(40);
search.setQuery(query);
返回结果:
{name=John, count=6000, title=标题9992, age=25, height=158}---->9992
{name=Alice, count=4000, title=标题9999, age=12, height=156}---->9999
{name=Tom, count=5000, title=标题9991, age=13, height=157}---->9991
{name=Tom, count=15500, title=测试, age=12, height=169}---->1
{name=Jamis, count=5400, title=标题9995, age=23, height=161}---->9995
4、根据前缀查询PrefixQueryBuilder(查询name前缀为t的文档)
PrefixQueryBuilder query = QueryBuilders.prefixQuery("name", "t");
search.setQuery(query);
返回结果:
{name=Tomas, count=7600, title=标题9996, age=54, height=162}---->9996
{name=Tom, count=5000, title=标题9991, age=13, height=157}---->9991
{name=Tom, count=15500, title=测试, age=12, height=169}---->1
5、WildcardQueryBuilder通配符查询,*表示0或多个,?表示1或多个(查询name以字母e结尾的文档)
WildcardQueryBuilder query = QueryBuilders.wildcardQuery("name", "*e");
search.setQuery(query);
返回结果:
{name=Rose, count=9000, title=标题9998, age=87, height=167}---->9998
{name=Alice, count=4000, title=标题9999, age=12, height=156}---->9999
6、RegexpQueryBuilder正则表达式查询(查询name为tom或tomas的文档)
RegexpQueryBuilder query = QueryBuilders.regexpQuery("name", "tom|tomas");
search.setQuery(query);
返回结果:
{name=Tomas, count=7600, title=标题9996, age=54, height=162}---->9996
{name=Tom, count=5000, title=标题9991, age=13, height=157}---->9991
{name=Tom, count=15500, title=测试, age=12, height=169}---->1
7、IdsQueryBuilder根据文档的id查询(查询id为9996和9992的文档)
IdsQueryBuilder query = QueryBuilders.idsQuery();
query.addIds("9996","9992");
search.setQuery(query);
返回结果:
{name=Tomas, count=7600, title=标题9996, age=54, height=162}---->9996
{name=John, count=6000, title=标题9992, age=25, height=158}---->9992