Elasticsearch查询之TermsFilterBuilder

Lucene在做大量term值查询时, 如果这值过多, 超1024个term的话, 会出现

TooManyClauses[maxClauseCount is set to 1024] 的异常,因此建议在term过多的情况下采用filter, 而不是query。

以下是该情形在ES中的测试。

 

 

Settings defaultSettings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).build();
		Settings finalSettings = ImmutableSettings.settingsBuilder().put(defaultSettings)
				.put("name", NetworkUtils.getLocalAddress().getHostName()).build();
		TransportClient tmp = new TransportClient(finalSettings);
		Client client = tmp.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
		//demo 100万数据
		for (int i = 0; i < 1000000; i++)
		{
			client.prepareIndex("test2", "book",String.valueOf(i)).setSource("bookid", String.valueOf(i), "booktype", String.valueOf(i%10000)).execute()
			.actionGet();
		}
		//demo 近1万个term
		String[] values = new String[10000];
		for (int i = 1; i < 10000; i++)
		{
			values[i] = String.valueOf(i);
		}
		//terms query
		//TermsQueryBuilder termQueryBuilder = new TermsQueryBuilder("booktype", values);
		TermsFilterBuilder termsFilterBuilder = new TermsFilterBuilder("booktype", values);
//		SearchResponse searchResponse = client.prepareSearch().setIndices("test2").setQuery(termQueryBuilder)
//				.setFrom(0).setSize(100).execute().actionGet();
		//terms filter
		SearchResponse searchResponse = client.prepareSearch().setIndices("test2").setQuery(QueryBuilders.matchAllQuery()).setFilter(termsFilterBuilder)
				.setFrom(0).setSize(100).execute().actionGet();
		SearchHits hits = searchResponse.getHits();
		System.out.println(hits.totalHits());
		for (SearchHit searchHit : hits)
		{
			System.out.println(searchHit.getId() + ":" + searchHit.getSource().get("booktype"));
		}

 上述结果会发现, 用TermsQueryBuilder查询的话, 会出现TooManyClauses的异常, 因为设置了9999个term值。因此,当term过多时,建议采用filter, 而不是query. 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值