Skip to content

Commit

Permalink
more general encoding for _search fixes neo4j-contrib#372 (neo4j-cont…
Browse files Browse the repository at this point in the history
  • Loading branch information
albertodelazzari committed Jun 28, 2017
1 parent a3d4916 commit 27a4e1d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/main/java/apoc/es/ElasticSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class ElasticSearch {

private final static String fullQueryTemplate = "/%s/%s/%s?%s";

// /{index}/{type}/_search?q={query}
private final static String fullQuerySearchTemplate = "/%s/%s/_search?q=%s";
// /{index}/{type}/_search?{query}
private final static String fullQuerySearchTemplate = "/%s/%s/_search?%s";

/**
* With this pattern we can match both key:value params and key=value params
Expand Down Expand Up @@ -74,10 +74,7 @@ private String formatSearchQueryUrl(String index, String type, Object query) {
type == null ? "_all" : type,
toQueryParams(query));

// We can leave the trailing "&" because is not a problem
queryUrl = queryUrl.endsWith("q=") ? queryUrl.substring(0, queryUrl.length() - 2) : queryUrl;

return queryUrl;
return queryUrl.endsWith("?") ? queryUrl.substring(0, queryUrl.length() - 1) : queryUrl;
}

/**
Expand Down
63 changes: 61 additions & 2 deletions src/test/java/apoc/es/ElasticSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.net.ConnectException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -189,20 +190,78 @@ public void testGetWithQueryAsStringSingleParam() throws Exception {
}, ConnectException.class);
}

/**
* We want to search our document by name --> /test-index/test-type/_search?
* This test uses a plain string to query ES
*/
@Test
public void testSearchWithQueryNull() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},null,null) yield value", defaultParams, r -> {
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.procedureName");
assertEquals("get", name);
});
}, ConnectException.class);
}

/**
* We want to search our document by name --> /test-index/test-type/_search?q=name:Neo4j
* This test uses a plain string to query ES
*/
@Test
public void testSearchWithQueryAsAString() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},'name:Neo4j',null) yield value", defaultParams, r -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},'q=name:Neo4j',null) yield value", defaultParams, r -> {
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.name");
assertEquals("Neo4j", name);
});
}, ConnectException.class);
}

/**
* We want to search our document by name --> /test-index/test-type/_search?q=name:*
* This test uses a plain string to query ES
*/
@Test
public void testFullSearchWithQueryAsAString() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},'q=name:*',null) yield value", defaultParams, r -> {
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.name");
assertEquals("Neo4j", name);
});
}, ConnectException.class);
}

/**
* We want to search our document by name --> /test-index/test-type/_search?q=*
* This test uses a plain string to query ES
*/
@Test
public void testFullSearchWithQueryAsAStringWithEquals() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},'q=*',null) yield value", defaultParams, r -> {
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.procedureName");
assertEquals("get", name);
});
}, ConnectException.class);
}

/**
* We want to search our document by name --> /test-index/test-type/_search?size=1&scroll=1m&_source=true
* This test uses a plain string to query ES
*/
@Test
public void testFullSearchWithOtherParametersAsAString() throws Exception {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query({host},{index},{type},'size=1&scroll=1m&_source=true',null) yield value", defaultParams, r -> {
Object hits = extractValueFromResponse(r, "$.hits.hits");
assertEquals(1, ((List) hits).size());
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.procedureName");
assertEquals("get", name);
});
}, ConnectException.class);
}

/**
* We want to add a field to an existing document posting a request with a payload
* http://localhost:9200/test-index/test-type/0b727048-a6ca-44f4-906b-f0e86ed65c7e + payload: {"event":"Graph Connect 2017"}
Expand Down Expand Up @@ -287,7 +346,7 @@ public void testPutNewDocument() {
@Test
public void testSearchWithQueryAsAMap() {
TestUtil.ignoreException(() -> {
TestUtil.testCall(db, "CALL apoc.es.query('" + HOST + "','" + ES_INDEX + "','" + ES_TYPE + "',{name:'Neo4j'},null) yield value", r -> {
TestUtil.testCall(db, "CALL apoc.es.query('" + HOST + "','" + ES_INDEX + "','" + ES_TYPE + "',{q:'name:Neo4j'},null) yield value", r -> {
Object name = extractValueFromResponse(r, "$.hits.hits[0]._source.name");
assertEquals("Neo4j", name);
});
Expand Down

0 comments on commit 27a4e1d

Please sign in to comment.