camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject camel git commit: [CAMEL-8506] Add SEARCH operation to Elasticsearch component
Date Wed, 18 Mar 2015 12:47:57 GMT
Repository: camel
Updated Branches:
  refs/heads/master 1e17b6b59 -> 014ecdb47


[CAMEL-8506] Add SEARCH operation to Elasticsearch component


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/014ecdb4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/014ecdb4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/014ecdb4

Branch: refs/heads/master
Commit: 014ecdb47d61d9f44776facb9e7ee270e74925f6
Parents: 1e17b6b
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Wed Mar 18 13:47:50 2015 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Wed Mar 18 13:47:50 2015 +0100

----------------------------------------------------------------------
 .../ElasticsearchConfiguration.java             |  1 +
 .../elasticsearch/ElasticsearchProducer.java    |  6 ++++++
 .../ElasticsearchActionRequestConverter.java    | 11 ++++++++++
 .../ElasticsearchComponentTest.java             | 21 ++++++++++++++++++++
 4 files changed, 39 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/014ecdb4/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java
index c38158c..7db78c7 100644
--- a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java
+++ b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConfiguration.java
@@ -39,6 +39,7 @@ public class ElasticsearchConfiguration {
     public static final String OPERATION_BULK_INDEX = "BULK_INDEX";
     public static final String OPERATION_GET_BY_ID = "GET_BY_ID";
     public static final String OPERATION_DELETE = "DELETE";
+    public static final String OPERATION_SEARCH = "SEARCH";
     public static final String PARAM_INDEX_ID = "indexId";
     public static final String PARAM_DATA = "data";
     public static final String PARAM_INDEX_NAME = "indexName";

http://git-wip-us.apache.org/repos/asf/camel/blob/014ecdb4/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java
index c60a1df..2c2647a 100644
--- a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java
+++ b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchProducer.java
@@ -28,6 +28,7 @@ import org.elasticsearch.action.bulk.BulkRequest;
 import org.elasticsearch.action.delete.DeleteRequest;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.client.Client;
 
 /**
@@ -69,6 +70,8 @@ public class ElasticsearchProducer extends DefaultProducer {
             }
         } else if (request instanceof DeleteRequest) {
             return ElasticsearchConfiguration.OPERATION_DELETE;
+        } else if (request instanceof SearchRequest) {
+            return ElasticsearchConfiguration.OPERATION_SEARCH;
         }
 
         String operationConfig = exchange.getIn().getHeader(ElasticsearchConfiguration.PARAM_OPERATION,
String.class);
@@ -131,6 +134,9 @@ public class ElasticsearchProducer extends DefaultProducer {
         } else if (ElasticsearchConfiguration.OPERATION_DELETE.equals(operation)) {
             DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
             message.setBody(client.delete(deleteRequest).actionGet());
+        } else if (ElasticsearchConfiguration.OPERATION_SEARCH.equals(operation)) {
+            SearchRequest searchRequest = message.getBody(SearchRequest.class);
+            message.setBody(client.search(searchRequest).actionGet());
         } else {
             throw new IllegalArgumentException(ElasticsearchConfiguration.PARAM_OPERATION
+ " value '" + operation + "' is not supported");
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/014ecdb4/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java
b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java
index a62f218..934258f 100644
--- a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java
+++ b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/converter/ElasticsearchActionRequestConverter.java
@@ -26,6 +26,7 @@ import org.elasticsearch.action.bulk.BulkRequest;
 import org.elasticsearch.action.delete.DeleteRequest;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 
 @Converter
@@ -85,6 +86,16 @@ public final class ElasticsearchActionRequestConverter {
     }
 
     @Converter
+    public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange) {
+        Map<?, ?> query = exchange.getContext().getTypeConverter().convertTo(Map.class,
queryObject);
+        return new SearchRequest(exchange.getIn().getHeader(
+                ElasticsearchConfiguration.PARAM_INDEX_NAME, String.class))
+                .types(exchange.getIn().getHeader(
+                        ElasticsearchConfiguration.PARAM_INDEX_TYPE,
+                        String.class)).source(query);
+    }
+
+    @Converter
     public static BulkRequest toBulkRequest(List<Object> documents,
                                             Exchange exchange) {
         BulkRequest request = new BulkRequest();

http://git-wip-us.apache.org/repos/asf/camel/blob/014ecdb4/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java
b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java
index 88b7299..55d8998 100644
--- a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java
+++ b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchComponentTest.java
@@ -30,6 +30,7 @@ import org.elasticsearch.action.delete.DeleteResponse;
 import org.elasticsearch.action.get.GetRequest;
 import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.search.SearchResponse;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -112,6 +113,25 @@ public class ElasticsearchComponentTest extends CamelTestSupport {
     }
 
     @Test
+    public void testSearch() throws Exception {
+        //first, INDEX a value
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("content", "testSearch");
+        sendBody("direct:index", map);
+
+        //now, verify GET succeeded
+        Map<String, Object> actualQuery = new HashMap<String, Object>();
+        actualQuery.put("content", "searchtest");
+        Map<String, Object> match = new HashMap<String, Object>();
+        match.put("match", actualQuery);
+        Map<String, Object> query = new HashMap<String, Object>();
+        query.put("query", match);
+        SearchResponse response = template.requestBody("direct:search", query, SearchResponse.class);
+        assertNotNull("response should not be null", response);
+        assertNotNull("response hits should be == 1", response.getHits().totalHits());
+    }
+
+    @Test
     public void testIndexWithHeaders() throws Exception {
         Map<String, String> map = new HashMap<String, String>();
         map.put("content", "test");
@@ -311,6 +331,7 @@ public class ElasticsearchComponentTest extends CamelTestSupport {
                 from("direct:index").to("elasticsearch://local?operation=INDEX&indexName=twitter&indexType=tweet");
                 from("direct:get").to("elasticsearch://local?operation=GET_BY_ID&indexName=twitter&indexType=tweet");
                 from("direct:delete").to("elasticsearch://local?operation=DELETE&indexName=twitter&indexType=tweet");
+                from("direct:search").to("elasticsearch://local?operation=SEARCH&indexName=twitter&indexType=tweet");
                 from("direct:bulk_index").to("elasticsearch://local?operation=BULK_INDEX&indexName=twitter&indexType=tweet");
                 from("direct:bulk").to("elasticsearch://local?operation=BULK&indexName=twitter&indexType=tweet");
                 //from("direct:indexWithIp").to("elasticsearch://elasticsearch?operation=INDEX&indexName=twitter&indexType=tweet&ip=localhost");


Mime
View raw message