Search: When fetching _parent, only the _id should be returned, and not type#id, closes #658.
This commit is contained in:
parent
8b9ec890c3
commit
e516051ea5
|
@ -116,6 +116,18 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements org.e
|
|||
return field.stringValue();
|
||||
}
|
||||
|
||||
@Override public Object valueForSearch(Fieldable field) {
|
||||
String fieldValue = field.stringValue();
|
||||
if (fieldValue == null) {
|
||||
return null;
|
||||
}
|
||||
int index = fieldValue.indexOf(Uid.DELIMITER);
|
||||
if (index == -1) {
|
||||
return fieldValue;
|
||||
}
|
||||
return fieldValue.substring(index + 1);
|
||||
}
|
||||
|
||||
@Override public String indexedValue(String value) {
|
||||
if (value.indexOf(Uid.DELIMITER) == -1) {
|
||||
return Uid.createUid(type, value);
|
||||
|
|
|
@ -80,9 +80,46 @@ public class SimpleChildQuerySearchTests extends AbstractNodesTests {
|
|||
|
||||
client.admin().indices().prepareRefresh().execute().actionGet();
|
||||
|
||||
// TEST FETCHING _parent from child
|
||||
SearchResponse searchResponse = client.prepareSearch("test")
|
||||
.setQuery(termQuery("child._id", "c1"))
|
||||
.addFields("_parent")
|
||||
.execute().actionGet();
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
logger.warn("Failed shards:");
|
||||
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
|
||||
logger.warn("-> {}", shardSearchFailure);
|
||||
}
|
||||
}
|
||||
assertThat(searchResponse.failedShards(), equalTo(0));
|
||||
assertThat(searchResponse.hits().totalHits(), equalTo(1l));
|
||||
assertThat(searchResponse.hits().getAt(0).id(), equalTo("c1"));
|
||||
assertThat(searchResponse.hits().getAt(0).field("_parent").value().toString(), equalTo("p1"));
|
||||
|
||||
// TEST matching on parent
|
||||
searchResponse = client.prepareSearch("test")
|
||||
.setQuery(termQuery("child._parent", "p1"))
|
||||
.addFields("_parent")
|
||||
.execute().actionGet();
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
logger.warn("Failed shards:");
|
||||
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
|
||||
logger.warn("-> {}", shardSearchFailure);
|
||||
}
|
||||
}
|
||||
assertThat(searchResponse.failedShards(), equalTo(0));
|
||||
assertThat(searchResponse.hits().totalHits(), equalTo(2l));
|
||||
assertThat(searchResponse.hits().getAt(0).id(), anyOf(equalTo("c1"), equalTo("c2")));
|
||||
assertThat(searchResponse.hits().getAt(0).field("_parent").value().toString(), equalTo("p1"));
|
||||
assertThat(searchResponse.hits().getAt(1).id(), anyOf(equalTo("c1"), equalTo("c2")));
|
||||
assertThat(searchResponse.hits().getAt(1).field("_parent").value().toString(), equalTo("p1"));
|
||||
|
||||
|
||||
// TOP CHILDREN QUERY
|
||||
|
||||
SearchResponse searchResponse = client.prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "yellow"))).execute().actionGet();
|
||||
searchResponse = client.prepareSearch("test")
|
||||
.setQuery(topChildrenQuery("child", termQuery("c_field", "yellow")))
|
||||
.execute().actionGet();
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
logger.warn("Failed shards:");
|
||||
for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
|
||||
|
|
Loading…
Reference in New Issue