我正在从名为replacement_local100
的Elastic Search索引中获取文档。我两次查询相同的索引。
第一次->基于code
作为输入,得到link_ids
的列表。
第二次->基于List of Link IDs
(来自第一个查询),我得到了不同的数据,我也正在使用过滤器。
当我调用第二个查询时,出现错误:
ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:573)
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:549)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:456)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:429)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:368)
at com.jci.vb2.searchengine.dao.ReplacementDao.getReplacementByCode(ReplacementDao.java:127)
at com.jci.vb2.searchengineservice.mqservice.Service.getReplacementByCode(Service.java:73)
at com.jci.vb2.searchengineservice.mqservice.Consumer.consume(Consumer.java:48)
at com.jci.vb2.utils.Consumer$1$1.run(Consumer.java:89)
at java.lang.Thread.run(Thread.java:748)
Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9200], URI [/replacement_local100/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"term\" : {\n \"id\" : {\n \"value\" : \"]\",\n \"boost\" : 1.0\n }\n }\n }\n ],\n \"filter\" : [\n {\n \"match\" : {\n \"product_link_type\" : {\n \"query\" : \"replacement\",\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n }\n ],\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}","index_uuid":"yg7rn4njRaKfC6YtraXJPg","index":"replacement_local100"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"replacement_local100","node":"uPLyU7R5RXeirg8XzRqhnA","reason":{"type":"query_shard_exception","reason":"failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"term\" : {\n \"id\" : {\n \"value\" : \"]\",\n \"boost\" : 1.0\n }\n }\n }\n ],\n \"filter\" : [\n {\n \"match\" : {\n \"product_link_type\" : {\n \"query\" : \"replacement\",\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n }\n ],\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}","index_uuid":"yg7rn4njRaKfC6YtraXJPg","index":"replacement_local100","caused_by":{"type":"number_format_exception","reason":"For input string: \"]\""}}}]},"status":400}
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:346)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
... 1 more
Suppressed: java.net.ConnectException
at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:168)
at org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:561)
at org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:822)
at org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:183)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:210)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:155)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:192)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
... 1 more
以下我用于从ElasticSearch索引中获取文档的Java代码。
private final String INDEX = "replacement_local100";
private final String TYPE = "doc";
private RestHighLevelClient restHighLevelClient;
private ObjectMapper objectMapper;
public ReplacementDao( ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
this.objectMapper = objectMapper;
this.restHighLevelClient = restHighLevelClient;
}
public List<Replacement> getReplacementByCode(String code, String regionId, String countryCode, String companyId){
System.out.println("replacement service...."+code);
try {
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
} catch (final Exception e) {
logger.info("Error closing ElasticSearch client: ");
}
try {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
} catch (Exception e) {
logger.info(e.getMessage());
}
SearchRequest searchRequest = new SearchRequest(INDEX);
SearchRequest searchRequest2 = new SearchRequest(INDEX);
searchRequest.types(TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
QueryBuilder qb = QueryBuilders.matchQuery("code", code);
searchSourceBuilder.query(qb);
searchSourceBuilder.size(50);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse1 = null;
try {
searchResponse1 = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.getLocalizedMessage();
}
SearchHit[] searchHits1 = searchResponse1.getHits().getHits();
System.out.println("Hits size--->"+searchHits1.length);
Replacement linkId = null;
Replacement replacement = null;
List<Replacement> linkIDList=new ArrayList<Replacement>();
for (SearchHit hit1 : searchHits1) {
linkId = new Replacement();
Map<String, Object> sourceAsMap1 = hit1.getSourceAsMap();
linkId.setLink_id((Integer) sourceAsMap1.get("link_id"));
linkIDList.add(linkId);
}
System.out.println("Link ID List Size --->"+linkIDList.size());
for(Replacement link_id:linkIDList) {
System.out.println("Link ID List --->"+link_id.getLink_id()+" Type--->"+link_id.getType());
}
QueryBuilder qb2=null;
List<Integer> link_id_array=new ArrayList<Integer>();
for(Replacement link_id:linkIDList) {
link_id_array.add(link_id.getLink_id());
}
qb2 = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("id", link_id_array))
.filter(QueryBuilders.matchQuery("product_link_type", "replacement"));
searchSourceBuilder2.query(qb2);
searchSourceBuilder2.size(50);
searchRequest2.source(searchSourceBuilder2);
System.out.println("Request 2 --->"+searchRequest2);
SearchResponse searchResponse2 = null;
try {
searchResponse2 = restHighLevelClient.search(searchRequest2); // Am getting error on this line
} catch (IOException e) {
e.getLocalizedMessage();
}
List<Replacement> replacementList=new ArrayList<Replacement>();
SearchHit[] searchHits2 = searchResponse2.getHits().getHits();
for (SearchHit hit2 : searchHits2) {
Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
replacement=new Replacement();
replacement.setId(sourceAsMap2.get("id").toString());
replacement.setCode(sourceAsMap2.get("code").toString());
replacementList.add(replacement);
}
答案 0 :(得分:3)
问题是这一行:
.must(QueryBuilders.termQuery("id", link_id_array))
由于要传递ID列表,因此您需要使用terms
查询
.must(QueryBuilders.termsQuery("id", link_id_array))
^
|
change this