This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 30203d6 CAMEL-13348 support to do the elasticsearch without specifying the indexName and indexType 30203d6 is described below commit 30203d69cc67f92313845bd26a063247bb4d7ea4 Author: Willem Jiang <jiangni...@huawei.com> AuthorDate: Wed Mar 20 19:47:39 2019 +0800 CAMEL-13348 support to do the elasticsearch without specifying the indexName and indexType --- .../converter/ElasticsearchActionRequestConverter.java | 18 ++++++++++++++---- .../ElasticsearchGetSearchDeleteExistsUpdateTest.java | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java index fbb3c43..0262821 100644 --- a/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java +++ b/components/camel-elasticsearch-rest/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.camel.Converter; import org.apache.camel.Exchange; import org.apache.camel.component.elasticsearch.ElasticsearchConstants; +import org.apache.camel.util.ObjectHelper; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.get.GetRequest; @@ -157,9 +158,17 @@ public final class ElasticsearchActionRequestConverter { if (queryObject instanceof SearchRequest) { return (SearchRequest) queryObject; } - SearchRequest searchRequest = new SearchRequest(exchange.getIn() - .getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class)) - .types(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class)); + SearchRequest searchRequest = new SearchRequest(); + + // Only setup the indexName and indexType if the message header has the setting + String indexName = exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class); + String indexType = exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class); + if (ObjectHelper.isNotEmpty(indexName)) { + searchRequest.indices(indexName); + } + if (ObjectHelper.isNotEmpty(indexType)) { + searchRequest.types(indexType); + } SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); String queryText = null; @@ -174,7 +183,7 @@ public final class ElasticsearchActionRequestConverter { XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); queryText = Strings.toString(contentBuilder.map(mapQuery)); } catch (IOException e) { - LOG.error(e.getMessage()); + LOG.error("Cannot build the QueryText from the map.", e); } } else if (queryObject instanceof String) { queryText = (String) queryObject; @@ -186,6 +195,7 @@ public final class ElasticsearchActionRequestConverter { } } else { // Cannot convert the queryObject into SearchRequest + LOG.info("Cannot convert queryObject into SearchRequest object"); return null; } diff --git a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java index 8b31644..15b0ce5 100644 --- a/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java +++ b/components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java @@ -92,7 +92,7 @@ public class ElasticsearchGetSearchDeleteExistsUpdateTest extends ElasticsearchB match.put("match", actualQuery); Map<String, Object> query = new HashMap<>(); query.put("query", match); - SearchHits response = template.requestBody("direct:search", match, SearchHits.class); + SearchHits response = template.requestBody("direct:search", query, SearchHits.class); assertNotNull("response should not be null", response); assertEquals("response hits should be == 0", 0, response.totalHits); } @@ -108,10 +108,17 @@ public class ElasticsearchGetSearchDeleteExistsUpdateTest extends ElasticsearchB GetResponse getResponse = template.requestBody("direct:get", indexId, GetResponse.class); assertNotNull("response should not be null", getResponse); assertNotNull("response source should not be null", getResponse.getSource()); - SearchRequest req = new SearchRequest(); - req.indices("twitter"); - req.types("tweet"); - SearchHits response = template.requestBody("direct:search", req, SearchHits.class); + // need to create a query string + String query = "{\n" + + " \"query\" : { \"match\" : { \"key\" : \"value\" }}\n" + + "}\n"; + SearchHits response = template.requestBody("direct:search", query, SearchHits.class); + assertNotNull("response should not be null", response); + assertEquals("response hits should be == 0", 0, response.totalHits); + + // testing + + response = template.requestBody("direct:search-1", query, SearchHits.class); assertNotNull("response should not be null", response); assertEquals("response hits should be == 0", 0, response.totalHits); } @@ -325,6 +332,7 @@ public class ElasticsearchGetSearchDeleteExistsUpdateTest extends ElasticsearchB from("direct:multiget").to("elasticsearch-rest://elasticsearch?operation=MultiGet&indexName=twitter&indexType=tweet&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); from("direct:delete").to("elasticsearch-rest://elasticsearch?operation=Delete&indexName=twitter&indexType=tweet&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); from("direct:search").to("elasticsearch-rest://elasticsearch?operation=Search&indexName=twitter&indexType=tweet&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); + from("direct:search-1").to("elasticsearch-rest://elasticsearch?operation=Search&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); from("direct:multiSearch").to("elasticsearch-rest://elasticsearch?operation=MultiSearch&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); from("direct:update").to("elasticsearch-rest://elasticsearch?operation=Update&indexName=twitter&indexType=tweet&hostAddresses=localhost:" + ES_BASE_HTTP_PORT); from("direct:exists").to("elasticsearch-rest://elasticsearch?operation=Exists&hostAddresses=localhost:" + ES_BASE_HTTP_PORT);