es之基本(常用)查询

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星***满天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值