Repository: camel Updated Branches: refs/heads/master 5f53ce5e9 -> 6aa8233cd
CAMEL-9441: Camel-Elasticsearch: Add an operation to know if an index exists or not Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6aa8233c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6aa8233c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6aa8233c Branch: refs/heads/master Commit: 6aa8233cd69cf75e64c97418d4b246ab95840799 Parents: 5f53ce5 Author: Andrea Cosentino <[email protected]> Authored: Tue Dec 22 12:58:00 2015 +0100 Committer: Andrea Cosentino <[email protected]> Committed: Tue Dec 22 12:58:00 2015 +0100 ---------------------------------------------------------------------- .../elasticsearch/ElasticsearchConstants.java | 1 + .../elasticsearch/ElasticsearchProducer.java | 6 + .../ElasticsearchActionRequestConverter.java | 7 + ...icsearchGetSearchDeleteExistsUpdateTest.java | 291 +++++++++++++++++++ .../ElasticsearchGetSearchDeleteUpdateTest.java | 271 ----------------- 5 files changed, 305 insertions(+), 271 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/6aa8233c/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConstants.java b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConstants.java index caae8c8..dab1cc8 100644 --- a/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConstants.java +++ b/components/camel-elasticsearch/src/main/java/org/apache/camel/component/elasticsearch/ElasticsearchConstants.java @@ -30,6 +30,7 @@ public interface ElasticsearchConstants { String OPERATION_MULTIGET = "MULTIGET"; String OPERATION_DELETE = "DELETE"; String OPERATION_SEARCH = "SEARCH"; + String OPERATION_EXISTS = "EXISTS"; String PARAM_INDEX_ID = "indexId"; String PARAM_DATA = "data"; String PARAM_INDEX_NAME = "indexName"; http://git-wip-us.apache.org/repos/asf/camel/blob/6aa8233c/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 9a54dde..fbb2f7f 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 @@ -25,6 +25,7 @@ import org.apache.camel.impl.DefaultProducer; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.exists.ExistsRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.index.IndexRequest; @@ -74,6 +75,8 @@ public class ElasticsearchProducer extends DefaultProducer { } } else if (request instanceof DeleteRequest) { return ElasticsearchConstants.OPERATION_DELETE; + } else if (request instanceof ExistsRequest) { + return ElasticsearchConstants.OPERATION_EXISTS; } else if (request instanceof SearchRequest) { return ElasticsearchConstants.OPERATION_SEARCH; } @@ -151,6 +154,9 @@ public class ElasticsearchProducer extends DefaultProducer { } else if (ElasticsearchConstants.OPERATION_DELETE.equals(operation)) { DeleteRequest deleteRequest = message.getBody(DeleteRequest.class); message.setBody(client.delete(deleteRequest).actionGet()); + } else if (ElasticsearchConstants.OPERATION_EXISTS.equals(operation)) { + ExistsRequest existsRequest = message.getBody(ExistsRequest.class); + message.setBody(client.exists(existsRequest).actionGet()); } else if (ElasticsearchConstants.OPERATION_SEARCH.equals(operation)) { SearchRequest searchRequest = message.getBody(SearchRequest.class); message.setBody(client.search(searchRequest).actionGet()); http://git-wip-us.apache.org/repos/asf/camel/blob/6aa8233c/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 d33420c..7c638ee 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.apache.camel.component.elasticsearch.ElasticsearchConstants; import org.elasticsearch.action.WriteConsistencyLevel; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.exists.ExistsRequest; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.get.MultiGetRequest.Item; @@ -118,6 +119,12 @@ public final class ElasticsearchActionRequestConverter { } @Converter + public static ExistsRequest toExistsRequest(String id, Exchange exchange) { + return new ExistsRequest(exchange.getIn().getHeader( + ElasticsearchConstants.PARAM_INDEX_NAME, String.class)); + } + + @Converter public static MultiGetRequest toMultiGetRequest(Object document, Exchange exchange) { List<Item> items = (List<Item>) document; MultiGetRequest multiGetRequest = new MultiGetRequest(); http://git-wip-us.apache.org/repos/asf/camel/blob/6aa8233c/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java ---------------------------------------------------------------------- diff --git a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java new file mode 100644 index 0000000..119c781 --- /dev/null +++ b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteExistsUpdateTest.java @@ -0,0 +1,291 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.elasticsearch; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.builder.RouteBuilder; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.exists.ExistsResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.get.MultiGetItemResponse; +import org.elasticsearch.action.get.MultiGetRequest.Item; +import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; + +public class ElasticsearchGetSearchDeleteExistsUpdateTest extends ElasticsearchBaseTest { + + @Test + public void testGet() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + sendBody("direct:index", map); + String indexId = template.requestBody("direct:index", map, String.class); + assertNotNull("indexId should be set", indexId); + + //now, verify GET succeeded + GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + } + + @Test + public void testDelete() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + sendBody("direct:index", map); + String indexId = template.requestBody("direct:index", map, String.class); + assertNotNull("indexId should be set", indexId); + + //now, verify GET succeeded + GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + + //now, perform DELETE + DeleteResponse deleteResponse = template.requestBody("direct:delete", indexId, DeleteResponse.class); + assertNotNull("response should not be null", deleteResponse); + + //now, verify GET fails to find the indexed value + response = template.requestBody("direct:get", indexId, GetResponse.class); + assertNotNull("response should not be null", response); + assertNull("response source should be null", response.getSource()); + } + + @Test + public void testSearch() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + 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 testUpdate() throws Exception { + Map<String, String> map = createIndexedData(); + String indexId = template.requestBody("direct:index", map, String.class); + assertNotNull("indexId should be set", indexId); + + Map<String, String> newMap = new HashMap<>(); + newMap.put(createPrefix() + "key2", createPrefix() + "value2"); + Map<String, Object> headers = new HashMap<>(); + headers.put(ElasticsearchConstants.PARAM_INDEX_ID, indexId); + indexId = template.requestBodyAndHeaders("direct:update", newMap, headers, String.class); + assertNotNull("indexId should be set", indexId); + } + + @Test + public void testGetWithHeaders() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); + + String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + + //now, verify GET + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); + GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + } + + @Test + public void testExistsWithHeaders() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); + + String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + + //now, verify GET + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_EXISTS); + ExistsResponse response = template.requestBodyAndHeaders("direct:exists", "", headers, ExistsResponse.class); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.exists()); + } + + @Test + public void testMultiGet() throws Exception { + //first, INDEX two values + Map<String, String> map = createIndexedData(); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); + headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "1"); + + template.requestBodyAndHeaders("direct:start", map, headers, String.class); + + headers.clear(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "facebook"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "status"); + headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "2"); + + template.requestBodyAndHeaders("direct:start", map, headers, String.class); + headers.clear(); + + //now, verify MULTIGET + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTIGET); + Item item1 = new Item("twitter", "tweet", "1"); + Item item2 = new Item("facebook", "status", "2"); + Item item3 = new Item("instagram", "latest", "3"); + List<Item> list = new ArrayList<Item>(); + list.add(item1); + list.add(item2); + list.add(item3); + MultiGetResponse response = template.requestBodyAndHeaders("direct:start", list, headers, MultiGetResponse.class); + MultiGetItemResponse[] responses = response.getResponses(); + assertNotNull("response should not be null", response); + assertEquals("response should contains three multiGetResponse object", 3, response.getResponses().length); + assertEquals("response 1 should contains tweet as type", "tweet", responses[0].getResponse().getType().toString()); + assertEquals("response 2 should contains status as type", "status", responses[1].getResponse().getType().toString()); + assertFalse("response 1 should be ok", responses[0].isFailed()); + assertFalse("response 2 should be ok", responses[1].isFailed()); + assertTrue("response 3 should be failed", responses[2].isFailed()); + } + + @Test + public void testDeleteWithHeaders() throws Exception { + //first, INDEX a value + Map<String, String> map = createIndexedData(); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); + + String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + + //now, verify GET + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); + GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + assertNotNull("response should not be null", response); + assertNotNull("response source should not be null", response.getSource()); + + //now, perform DELETE + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_DELETE); + DeleteResponse deleteResponse = template.requestBodyAndHeaders("direct:start", indexId, headers, DeleteResponse.class); + assertNotNull("response should not be null", deleteResponse); + + //now, verify GET fails to find the indexed value + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); + response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + assertNotNull("response should not be null", response); + assertNull("response source should be null", response.getSource()); + } + + @Test + public void testUpdateWithIDInHeader() throws Exception { + Map<String, String> map = createIndexedData(); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); + headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); + headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); + headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "123"); + + String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + assertNotNull("indexId should be set", indexId); + assertEquals("indexId should be equals to the provided id", "123", indexId); + + headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_UPDATE); + + indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + assertNotNull("indexId should be set", indexId); + assertEquals("indexId should be equals to the provided id", "123", indexId); + } + + @Test + public void getRequestBody() throws Exception { + String prefix = createPrefix(); + + // given + GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar"); + + // when + String documentId = template.requestBody("direct:index", + new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId") + .source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class); + GetResponse response = template.requestBody("direct:get", + request.id(documentId), GetResponse.class); + + // then + assertThat(response, notNullValue()); + assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content"))); + } + + @Test + public void deleteRequestBody() throws Exception { + String prefix = createPrefix(); + + // given + DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar"); + + // when + String documentId = template.requestBody("direct:index", + new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId") + .source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class); + DeleteResponse response = template.requestBody("direct:delete", + request.id(documentId), DeleteResponse.class); + + // then + assertThat(response, notNullValue()); + assertThat(documentId, equalTo(response.getId())); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to("elasticsearch://local"); + 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:multiget").to("elasticsearch://local?operation=MULTIGET&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:update").to("elasticsearch://local?operation=UPDATE&indexName=twitter&indexType=tweet"); + from("direct:exists").to("elasticsearch://local?operation=EXISTS"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/6aa8233c/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteUpdateTest.java ---------------------------------------------------------------------- diff --git a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteUpdateTest.java b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteUpdateTest.java deleted file mode 100644 index 75ddc56..0000000 --- a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchGetSearchDeleteUpdateTest.java +++ /dev/null @@ -1,271 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.elasticsearch; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.camel.builder.RouteBuilder; -import org.elasticsearch.action.delete.DeleteRequest; -import org.elasticsearch.action.delete.DeleteResponse; -import org.elasticsearch.action.get.GetRequest; -import org.elasticsearch.action.get.GetResponse; -import org.elasticsearch.action.get.MultiGetItemResponse; -import org.elasticsearch.action.get.MultiGetRequest.Item; -import org.elasticsearch.action.get.MultiGetResponse; -import org.elasticsearch.action.index.IndexRequest; -import org.elasticsearch.action.search.SearchResponse; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.notNullValue; - -public class ElasticsearchGetSearchDeleteUpdateTest extends ElasticsearchBaseTest { - - @Test - public void testGet() throws Exception { - //first, INDEX a value - Map<String, String> map = createIndexedData(); - sendBody("direct:index", map); - String indexId = template.requestBody("direct:index", map, String.class); - assertNotNull("indexId should be set", indexId); - - //now, verify GET succeeded - GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class); - assertNotNull("response should not be null", response); - assertNotNull("response source should not be null", response.getSource()); - } - - @Test - public void testDelete() throws Exception { - //first, INDEX a value - Map<String, String> map = createIndexedData(); - sendBody("direct:index", map); - String indexId = template.requestBody("direct:index", map, String.class); - assertNotNull("indexId should be set", indexId); - - //now, verify GET succeeded - GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class); - assertNotNull("response should not be null", response); - assertNotNull("response source should not be null", response.getSource()); - - //now, perform DELETE - DeleteResponse deleteResponse = template.requestBody("direct:delete", indexId, DeleteResponse.class); - assertNotNull("response should not be null", deleteResponse); - - //now, verify GET fails to find the indexed value - response = template.requestBody("direct:get", indexId, GetResponse.class); - assertNotNull("response should not be null", response); - assertNull("response source should be null", response.getSource()); - } - - @Test - public void testSearch() throws Exception { - //first, INDEX a value - Map<String, String> map = createIndexedData(); - 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 testUpdate() throws Exception { - Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); - assertNotNull("indexId should be set", indexId); - - Map<String, String> newMap = new HashMap<>(); - newMap.put(createPrefix() + "key2", createPrefix() + "value2"); - Map<String, Object> headers = new HashMap<>(); - headers.put(ElasticsearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", newMap, headers, String.class); - assertNotNull("indexId should be set", indexId); - } - - @Test - public void testGetWithHeaders() throws Exception { - //first, INDEX a value - Map<String, String> map = createIndexedData(); - Map<String, Object> headers = new HashMap<String, Object>(); - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); - headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); - headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); - - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); - - //now, verify GET - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); - GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); - assertNotNull("response should not be null", response); - assertNotNull("response source should not be null", response.getSource()); - } - - @Test - public void testMultiGet() throws Exception { - //first, INDEX two values - Map<String, String> map = createIndexedData(); - Map<String, Object> headers = new HashMap<String, Object>(); - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); - headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); - headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); - headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "1"); - - template.requestBodyAndHeaders("direct:start", map, headers, String.class); - - headers.clear(); - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); - headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "facebook"); - headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "status"); - headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "2"); - - template.requestBodyAndHeaders("direct:start", map, headers, String.class); - headers.clear(); - - //now, verify MULTIGET - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTIGET); - Item item1 = new Item("twitter", "tweet", "1"); - Item item2 = new Item("facebook", "status", "2"); - Item item3 = new Item("instagram", "latest", "3"); - List<Item> list = new ArrayList<Item>(); - list.add(item1); - list.add(item2); - list.add(item3); - MultiGetResponse response = template.requestBodyAndHeaders("direct:start", list, headers, MultiGetResponse.class); - MultiGetItemResponse[] responses = response.getResponses(); - assertNotNull("response should not be null", response); - assertEquals("response should contains three multiGetResponse object", 3, response.getResponses().length); - assertEquals("response 1 should contains tweet as type", "tweet", responses[0].getResponse().getType().toString()); - assertEquals("response 2 should contains status as type", "status", responses[1].getResponse().getType().toString()); - assertFalse("response 1 should be ok", responses[0].isFailed()); - assertFalse("response 2 should be ok", responses[1].isFailed()); - assertTrue("response 3 should be failed", responses[2].isFailed()); - } - - @Test - public void testDeleteWithHeaders() throws Exception { - //first, INDEX a value - Map<String, String> map = createIndexedData(); - Map<String, Object> headers = new HashMap<String, Object>(); - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); - headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); - headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); - - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); - - //now, verify GET - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); - GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); - assertNotNull("response should not be null", response); - assertNotNull("response source should not be null", response.getSource()); - - //now, perform DELETE - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_DELETE); - DeleteResponse deleteResponse = template.requestBodyAndHeaders("direct:start", indexId, headers, DeleteResponse.class); - assertNotNull("response should not be null", deleteResponse); - - //now, verify GET fails to find the indexed value - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID); - response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); - assertNotNull("response should not be null", response); - assertNull("response source should be null", response.getSource()); - } - - @Test - public void testUpdateWithIDInHeader() throws Exception { - Map<String, String> map = createIndexedData(); - Map<String, Object> headers = new HashMap<String, Object>(); - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX); - headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter"); - headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet"); - headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "123"); - - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); - assertNotNull("indexId should be set", indexId); - assertEquals("indexId should be equals to the provided id", "123", indexId); - - headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_UPDATE); - - indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); - assertNotNull("indexId should be set", indexId); - assertEquals("indexId should be equals to the provided id", "123", indexId); - } - - @Test - public void getRequestBody() throws Exception { - String prefix = createPrefix(); - - // given - GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar"); - - // when - String documentId = template.requestBody("direct:index", - new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId") - .source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class); - GetResponse response = template.requestBody("direct:get", - request.id(documentId), GetResponse.class); - - // then - assertThat(response, notNullValue()); - assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content"))); - } - - @Test - public void deleteRequestBody() throws Exception { - String prefix = createPrefix(); - - // given - DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar"); - - // when - String documentId = template.requestBody("direct:index", - new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId") - .source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class); - DeleteResponse response = template.requestBody("direct:delete", - request.id(documentId), DeleteResponse.class); - - // then - assertThat(response, notNullValue()); - assertThat(documentId, equalTo(response.getId())); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() { - from("direct:start").to("elasticsearch://local"); - 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:multiget").to("elasticsearch://local?operation=MULTIGET&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:update").to("elasticsearch://local?operation=UPDATE&indexName=twitter&indexType=tweet"); - } - }; - } -}
