分布式搜索Elasticsearch——QueryBuilders.idsQuery

        注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。

        ES源代码中对idsQuery的描述如下所示:

    /**
     * Constructs a query that will match only specific ids within types.
     *
     * @param types The mapping/doc type
     */
    public static IdsQueryBuilder idsQuery(@Nullable String... types) {
        return new IdsQueryBuilder(types);
    }

        即 指定type和id进行查询

        示例代码如下所示:

/**
 * @author Geloin
 */
package com.gsoft.gsearch.util;

import junit.framework.Assert;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Test;

import com.gsoft.gsearch.BaseTest;
import com.gsoft.gsearch.entity.Person;

/**
 * @author Geloin
 *
 */
public class IdsQueryTest extends BaseTest {
	@Test
	public void idsQuery() {
		try {
			
			String id1 = "udisjkdfd";
			String id2 = "ewdsfdsfe";

			BulkRequestBuilder builder = client.prepareBulk();
			// 创建索引
			Person p = new Person();
			p.setAge(20);
			p.setId(id1);
			p.setIsStudent(true);
			p.setName("张三的故事" + Math.random());
			p.setSex("男");

			String source = ElasticSearchUtil.BeanToJson(p);

			IndexRequest request = client.prepareIndex(index, type, p.getId())
					.setSource(source).request();

			builder.add(request);

			Person p2 = new Person();
			p2.setAge(20);
			p2.setId(id2);
			p2.setIsStudent(true);
			p2.setName("小李四讲的李四的故事" + Math.random());
			p2.setSex("男");

			String source2 = ElasticSearchUtil.BeanToJson(p2);

			IndexRequest request2 = client
					.prepareIndex(index, type, p2.getId()).setSource(source2)
					.request();

			builder.add(request2);

			BulkResponse response = builder.execute().actionGet();
			if (response.hasFailures()) {
				Assert.fail("创建索引失败!");
			}

			// 检索
			QueryBuilder qb = QueryBuilders.idsQuery(type).ids(id1, id2);

			SearchResponse sr = client.prepareSearch(index).setTypes(type)
					.setQuery(qb).setFrom(0).setSize(12).execute().actionGet();

			SearchHits hits = sr.getHits();

			if (null != hits && hits.totalHits() > 0) {
				for (SearchHit hit : hits) {
					String json = hit.getSourceAsString();

					Person newPerson = mapper.readValue(json, Person.class);
					System.out.println("id\t\t" + newPerson.getId());
					System.out.println("name\t\t" + newPerson.getName());
					System.out.println("sex\t\t" + newPerson.getSex());
					System.out.println("age\t\t" + newPerson.getAge());
					System.out.println("isStudent\t\t"
							+ newPerson.getIsStudent());
				}
			} else {
				log.error("没有查询到任何内容!");
				return;
			}

			// 查询总数量
			long count = client.prepareCount(index).setTypes(type).setQuery(qb)
					.execute().actionGet().count();

			log.info("符合条件的总数量为:" + count);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != client) {
				client.close();
			}
			if (null != node) {
				node.close();
			}
		}
	}
}

        请注意:必须在生成idsQuery后为其设定id ,设定id的方法有以下两种:

    /**
     * Adds ids to the filter.
     */
    public IdsQueryBuilder addIds(String... ids) {
        values.addAll(Arrays.asList(ids));
        return this;
    }

    /**
     * Adds ids to the filter.
     */
    public IdsQueryBuilder ids(String... ids) {
        return addIds(ids);
    }


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值