Skip to content

Commit

Permalink
Changes & Additions in PR searchbox-io#438. MultiSearch getter now ca…
Browse files Browse the repository at this point in the history
…n return 2 more parameter (ROUTING & SEARCH_TYPE). Test cases added to check new parameter additions. New parameters added to Parameters class.
  • Loading branch information
musabgelisgen committed Aug 9, 2018
1 parent 9059079 commit 7f052ed
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
10 changes: 7 additions & 3 deletions jest-common/src/main/java/io/searchbox/core/MultiSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.searchbox.action.AbstractAction;
import io.searchbox.action.GenericResultAbstractAction;
import io.searchbox.client.config.ElasticsearchVersion;
import io.searchbox.params.Parameters;
import io.searchbox.strings.StringUtils;

import java.util.Collection;
Expand Down Expand Up @@ -52,9 +53,12 @@ public String getData(Gson gson) {
if (StringUtils.isNotBlank(search.getType())) {
sb.append("\", \"type\" : \"").append(search.getType());
}
sb.append(getParameter(search, "ignore_unavailable"));
sb.append(getParameter(search, "allow_no_indices"));
sb.append(getParameter(search, "expand_wildcards"));
sb.append(getParameter(search, Parameters.IGNORE_UNAVAILABLE));
sb.append(getParameter(search, Parameters.ALLOW_NO_INDICES));
sb.append(getParameter(search, Parameters.EXPAND_WILDCARDS));
sb.append(getParameter(search, Parameters.ROUTING));
sb.append(getParameter(search, Parameters.SEARCH_TYPE));

final String query = NEWLINE_MATCHER.removeFrom(search.getData(gson));
sb.append("\"}\n")
.append(query)
Expand Down
3 changes: 3 additions & 0 deletions jest-common/src/main/java/io/searchbox/params/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ private Parameters() {
// 'true' | 'false'
public static final String ALLOW_NO_INDICES = "allow_no_indices";

// 'true' | 'false'
public static final String EXPAND_WILDCARDS = "expand_wildcards";

//'quorum' | 'one' | 'all'
public static final String CONSISTENCY = "consistency";

Expand Down
33 changes: 31 additions & 2 deletions jest-common/src/test/java/io/searchbox/core/MultiSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import io.searchbox.client.config.ElasticsearchVersion;
import io.searchbox.params.Parameters;
import io.searchbox.params.SearchType;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand Down Expand Up @@ -41,7 +43,7 @@ public void singleMultiSearchWithoutIndex() throws JSONException {
}

@Test
public void singleMultiSearchWitIndex() throws JSONException {
public void singleMultiSearchWithIndex() throws JSONException {
String expectedData = " {\"index\" : \"twitter\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n";
Search search = new Search.Builder("{\"query\" : {\"match_all\" : {}}}")
Expand All @@ -56,7 +58,7 @@ public void singleMultiSearchWitIndex() throws JSONException {
}

@Test
public void multiSearchWitIndex() throws JSONException {
public void multiSearchWithIndex() throws JSONException {
String expectedData = " {\"index\" : \"twitter\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n" +
"{\"index\" : \"_all\"}\n" +
Expand All @@ -73,6 +75,33 @@ public void multiSearchWitIndex() throws JSONException {
JSONAssert.assertEquals(expectedData, multiSearch.getData(null).toString(), false);
}

@Test
public void multiSearchWithExtraParameters() throws JSONException {
String expectedData = " {\"index\" : \"twitter\", " +
"\"search_type\" : \"query_then_fetch\"," +
" \"routing\" : \"testRoute\"," +
" \"ignore_unavailable\" : \"true\"," +
" \"allow_no_indices\" : \"true\", " +
"\"expand_wildcards\" : \"true\"}\n" +
"{\"query\" : {\"match_all\" : {}}}\n";

Search search = new Search.Builder("{\"query\" : {\"match_all\" : {}}}")
.addIndex("twitter")
.setParameter(Parameters.ROUTING, "testRoute")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setParameter(Parameters.IGNORE_UNAVAILABLE, true)
.setParameter(Parameters.ALLOW_NO_INDICES, true)
.setParameter(Parameters.EXPAND_WILDCARDS, true)
.build();
Search search2 = new Search.Builder("{\"query\" : {\"match_all\" : {}}}").build();

MultiSearch multiSearch = new MultiSearch.Builder(search).addSearch(search2).build();

assertEquals("POST", multiSearch.getRestMethodName());
assertEquals("/_msearch", multiSearch.getURI(ElasticsearchVersion.UNKNOWN));
JSONAssert.assertEquals(expectedData, multiSearch.getData(null), false);
}

@Test
public void equals() {
Search search1 = new Search.Builder("{\"match_all\" : {}}")
Expand Down

0 comments on commit 7f052ed

Please sign in to comment.