Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GetField behavior more consitent for multivalued fields. #3015

Merged
merged 1 commit into from May 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/main/java/org/elasticsearch/index/get/ShardGetService.java
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.get;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.apache.lucene.index.Term;
import org.elasticsearch.ElasticSearchException;
Expand Down Expand Up @@ -267,12 +268,11 @@ public GetResult innerGet(String type, String id, String[] gFields, boolean real
if (fields == null) {
fields = newHashMapWithExpectedSize(2);
}
GetField getField = fields.get(field);
if (getField == null) {
getField = new GetField(field, new ArrayList<Object>(2));
fields.put(field, getField);
if (value instanceof List) {
fields.put(field, new GetField(field, (List) value));
} else {
fields.put(field, new GetField(field, ImmutableList.of(value)));
}
getField.getValues().add(value);
}
}
}
Expand Down Expand Up @@ -379,12 +379,11 @@ private GetResult innerGetLoadFromStoredFields(String type, String id, String[]
if (fields == null) {
fields = newHashMapWithExpectedSize(2);
}
GetField getField = fields.get(field);
if (getField == null) {
getField = new GetField(field, new ArrayList<Object>(2));
fields.put(field, getField);
if (value instanceof List) {
fields.put(field, new GetField(field, (List) value));
} else {
fields.put(field, new GetField(field, ImmutableList.of(value)));
}
getField.getValues().add(value);
}
}
}
Expand Down
Expand Up @@ -276,19 +276,19 @@ public void getFieldsWithDifferentTypes() throws Exception {
GetResponse getResponse = client.prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
assertThat((List<String>) getResponse.getField("strs").getValue(), contains("A", "B", "C"));
assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C"));
assertThat((Long) getResponse.getField("int").getValue(), equalTo(42l));
assertThat((List<Long>) getResponse.getField("ints").getValue(), contains(1L, 2L, 3L, 4L));
assertThat(getResponse.getField("ints").getValues(), contains((Object) 1L, 2L, 3L, 4L));
assertThat((String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
assertThat(getResponse.getField("binary").getValue(), instanceOf(String.class)); // its a String..., not binary mapped

logger.info("--> realtime get (from stored fields)");
getResponse = client.prepareGet("test", "type2", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
assertThat((List<String>) getResponse.getField("strs").getValue(), contains("A", "B", "C"));
assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C"));
assertThat((Integer) getResponse.getField("int").getValue(), equalTo(42));
assertThat((List<Integer>) getResponse.getField("ints").getValue(), contains(1, 2, 3, 4));
assertThat(getResponse.getField("ints").getValues(), contains((Object) 1, 2, 3, 4));
assertThat((String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
assertThat((BytesReference) getResponse.getField("binary").getValue(), equalTo((BytesReference) new BytesArray(new byte[]{1, 2, 3})));

Expand All @@ -299,9 +299,9 @@ public void getFieldsWithDifferentTypes() throws Exception {
getResponse = client.prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat((String) getResponse.getField("str").getValue(), equalTo("test"));
assertThat((List<String>) getResponse.getField("strs").getValue(), contains("A", "B", "C"));
assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C"));
assertThat((Long) getResponse.getField("int").getValue(), equalTo(42l));
assertThat((List<Long>) getResponse.getField("ints").getValue(), contains(1L, 2L, 3L, 4L));
assertThat(getResponse.getField("ints").getValues(), contains((Object) 1L, 2L, 3L, 4L));
assertThat((String) getResponse.getField("date").getValue(), equalTo("2012-11-13T15:26:14.000Z"));
assertThat(getResponse.getField("binary").getValue(), instanceOf(String.class)); // its a String..., not binary mapped

Expand Down Expand Up @@ -364,10 +364,9 @@ public void testGetDocWithMultivaluedFields() throws Exception {
assertThat(response.getId(), equalTo("1"));
assertThat(response.getType(), equalTo("type1"));
assertThat(response.getFields().size(), equalTo(1));
assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(0).toString(), equalTo("1"));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(1).toString(), equalTo("2"));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));


response = client.prepareGet("test", "type2", "1")
Expand All @@ -377,10 +376,9 @@ public void testGetDocWithMultivaluedFields() throws Exception {
assertThat(response.getType(), equalTo("type2"));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getFields().size(), equalTo(1));
assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(0).toString(), equalTo("1"));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(1).toString(), equalTo("2"));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));

// Now test values being fetched from stored fields.
client.admin().indices().prepareRefresh("test").execute().actionGet();
Expand All @@ -390,10 +388,9 @@ public void testGetDocWithMultivaluedFields() throws Exception {
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getFields().size(), equalTo(1));
assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(0).toString(), equalTo("1"));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(1).toString(), equalTo("2"));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));


response = client.prepareGet("test", "type2", "1")
Expand All @@ -402,10 +399,9 @@ public void testGetDocWithMultivaluedFields() throws Exception {
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getFields().size(), equalTo(1));
assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(0).toString(), equalTo("1"));
assertThat(((List) response.getFields().get("field").getValues().get(0)).get(1).toString(), equalTo("2"));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
}

@Test
Expand Down