Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
added 'routing' and 'source' fields to MultiGet request helper class …
Browse files Browse the repository at this point in the history
…Doc - fixes #161
  • Loading branch information
Cihat Keser committed Nov 17, 2014
1 parent 01b34a2 commit 369a4b8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
45 changes: 44 additions & 1 deletion jest-common/src/main/java/io/searchbox/core/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Doc {

private final String id;

private String routing;

private Object source;

private final Collection<String> fields = new LinkedList<String>();

public Doc(String index, String type, String id) {
Expand Down Expand Up @@ -52,14 +56,46 @@ public Collection<String> getFields() {
return fields;
}

/**
* Specific stored fields can be specified to be retrieved per document to get,
* similar to the fields parameter of the Get API.
*/
public void addFields(Collection<String> fields) {
this.fields.addAll(fields);
}

/**
* Specific stored fields can be specified to be retrieved per document to get,
* similar to the fields parameter of the Get API.
*/
public void addField(String field) {
fields.add(field);
}

public void setRouting(String routing) {
this.routing = routing;
}

public String getRouting() {
return routing;
}

/**
* By default, the _source field will be returned for every document (if stored).
* Similar to the get API, you can retrieve only parts of the _source (or not at all)
* by using the _source parameter. You can also use the url parameters _source,
* _source_include & _source_exclude to specify defaults, which will be used when there
* are no per-document instructions.
*
*/
public void setSource(Object source) {
this.source = source;
}

public Object getSource() {
return source;
}

protected Map<String, Object> toMap() {
Map<String, Object> retval = new HashMap<String, Object>();

Expand All @@ -71,7 +107,14 @@ protected Map<String, Object> toMap() {
retval.put("fields", fields);
}

if(StringUtils.isNotEmpty(routing)){
retval.put("_routing", routing);
}

if(source != null) {
retval.put("_source", source);
}

return retval;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.searchbox.core;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.searchbox.action.Action;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
Expand All @@ -25,11 +26,52 @@ public class MultiGetIntegrationTest extends AbstractIntegrationTest {

@Before
public void setup() throws IOException {
client().index(new IndexRequest(TEST_INDEX, TEST_TYPE, "1").source("{\"text\":\"pumpkin\"}")).actionGet();
client().index(new IndexRequest(TEST_INDEX, TEST_TYPE, "1").source("{\"text\":\"pumpkin\", \"author\":\"anon\"}")).actionGet();
client().index(new IndexRequest(TEST_INDEX, TEST_TYPE, "2").source("{\"text\":\"spice\"}")).actionGet();
client().index(new IndexRequest(TEST_INDEX, TEST_TYPE, "3").source("{\"text\":\"latte\"}")).actionGet();
}

@Test
public void getWithPartialSource() throws IOException {
Doc doc1 = new Doc(TEST_INDEX, TEST_TYPE, "1");
doc1.setSource("author");

Action action = new MultiGet.Builder.ByDoc(doc1).build();
JestResult result = client.execute(action);
assertTrue(result.getErrorMessage(), result.isSucceeded());
result.getJsonObject().getAsJsonArray("docs");

JsonArray actualDocs = result.getJsonObject().getAsJsonArray("docs");
assertEquals("Number of docs in response should match the number of docs in requests.", 1, actualDocs.size());

JsonObject actualDoc1 = actualDocs.get(0).getAsJsonObject();
assertTrue("Document 1 is indexed and should have been found by the MultiGet request.",
actualDoc1.getAsJsonPrimitive("found").getAsBoolean());
JsonObject actualSource = actualDoc1.getAsJsonObject("_source");
assertNotNull("Response doc should have source", actualSource);
assertNull("Response doc source should not have unrequested text field", actualSource.get("text"));
assertNotNull("Response doc source should have requested author field", actualSource.get("author"));
}

@Test
public void getWithoutSource() throws IOException {
Doc doc1 = new Doc(TEST_INDEX, TEST_TYPE, "1");
doc1.setSource(Boolean.FALSE);

Action action = new MultiGet.Builder.ByDoc(doc1).build();
JestResult result = client.execute(action);
assertTrue(result.getErrorMessage(), result.isSucceeded());
result.getJsonObject().getAsJsonArray("docs");

JsonArray actualDocs = result.getJsonObject().getAsJsonArray("docs");
assertEquals("Number of docs in response should match the number of docs in requests.", 1, actualDocs.size());

JsonObject actualDoc1 = actualDocs.get(0).getAsJsonObject();
assertTrue("Document 1 is indexed and should have been found by the MultiGet request.",
actualDoc1.getAsJsonPrimitive("found").getAsBoolean());
assertNull("Response doc should not have source", actualDoc1.get("_source"));
}

@Test
public void getMultipleDocsWhenAllIndexedDocsAreRequested() throws IOException {
Doc doc1 = new Doc(TEST_INDEX, TEST_TYPE, "1");
Expand Down

0 comments on commit 369a4b8

Please sign in to comment.