use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testRandomParentChild.
public void testRandomParentChild() throws Exception {
assertAcked(prepareCreate("idx").addMapping("parent").addMapping("child1", "_parent", "type=parent").addMapping("child2", "_parent", "type=parent"));
int numDocs = scaledRandomIntBetween(5, 50);
List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
int child1 = 0;
int child2 = 0;
int[] child1InnerObjects = new int[numDocs];
int[] child2InnerObjects = new int[numDocs];
for (int parent = 0; parent < numDocs; parent++) {
String parentId = String.format(Locale.ENGLISH, "%03d", parent);
requestBuilders.add(client().prepareIndex("idx", "parent", parentId).setSource("{}", XContentType.JSON));
int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
int limit = child1 + numChildDocs;
for (; child1 < limit; child1++) {
requestBuilders.add(client().prepareIndex("idx", "child1", String.format(Locale.ENGLISH, "%04d", child1)).setParent(parentId).setSource("{}", XContentType.JSON));
}
numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
limit = child2 + numChildDocs;
for (; child2 < limit; child2++) {
requestBuilders.add(client().prepareIndex("idx", "child2", String.format(Locale.ENGLISH, "%04d", child2)).setParent(parentId).setSource("{}", XContentType.JSON));
}
}
indexRandom(true, requestBuilders);
int size = randomIntBetween(0, numDocs);
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).setTypes("parent").addSort("_uid", SortOrder.ASC).setQuery(boolQuery).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, numDocs);
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
int offset1 = 0;
int offset2 = 0;
for (int parent = 0; parent < numDocs; parent++) {
SearchHit searchHit = searchResponse.getHits().getAt(parent);
assertThat(searchHit.getType(), equalTo("parent"));
assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
assertThat(searchHit.getShard(), notNullValue());
SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.getTotalHits(), equalTo((long) child1InnerObjects[parent]));
for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child1"));
String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
assertThat(innerHit.getId(), equalTo(childId));
assertThat(innerHit.getNestedIdentity(), nullValue());
}
offset1 += child1InnerObjects[parent];
inner = searchHit.getInnerHits().get("b");
assertThat(inner.getTotalHits(), equalTo((long) child2InnerObjects[parent]));
for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
SearchHit innerHit = inner.getAt(child);
assertThat(innerHit.getType(), equalTo("child2"));
String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
assertThat(innerHit.getId(), equalTo(childId));
assertThat(innerHit.getNestedIdentity(), nullValue());
}
offset2 += child2InnerObjects[parent];
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testTypeIsAppliedInHasParentInnerQuery.
public void testTypeIsAppliedInHasParentInnerQuery() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent"));
ensureGreen();
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
indexRequests.add(client().prepareIndex("test", "parent", "1").setSource("field1", "a"));
indexRequests.add(client().prepareIndex("test", "child", "1").setParent("1").setSource("{}", XContentType.JSON));
indexRequests.add(client().prepareIndex("test", "child", "2").setParent("1").setSource("{}", XContentType.JSON));
indexRandom(true, indexRequests);
SearchResponse searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentQuery("parent", boolQuery().mustNot(termQuery("field1", "a")), false))).get();
assertHitCount(searchResponse, 0L);
searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", constantScoreQuery(boolQuery().mustNot(termQuery("field1", "a"))), false)).get();
assertHitCount(searchResponse, 0L);
searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("field1", "a"), false))).get();
assertHitCount(searchResponse, 2L);
searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", constantScoreQuery(termQuery("field1", "a")), false)).get();
assertHitCount(searchResponse, 2L);
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testMinMaxChildren.
public void testMinMaxChildren() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent", "id", "type=long").addMapping("child", "_parent", "type=parent"));
ensureGreen();
indexRandom(true, createMinMaxDocBuilders().toArray(new IndexRequestBuilder[0]));
SearchResponse response;
// Score mode = NONE
response = minMaxQuery(ScoreMode.None, 0, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 1, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 2, null);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 3, null);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 4, null);
assertThat(response.getHits().getTotalHits(), equalTo(0L));
response = minMaxQuery(ScoreMode.None, 0, 4);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 0, 3);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 0, 2);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.None, 2, 2);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1f));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.None, 3, 2));
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
// Score mode = SUM
response = minMaxQuery(ScoreMode.Total, 0, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Total, 1, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Total, 2, null);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
response = minMaxQuery(ScoreMode.Total, 3, null);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
response = minMaxQuery(ScoreMode.Total, 4, null);
assertThat(response.getHits().getTotalHits(), equalTo(0L));
response = minMaxQuery(ScoreMode.Total, 0, 4);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Total, 0, 3);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Total, 0, 2);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Total, 2, 2);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Total, 3, 2));
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
// Score mode = MAX
response = minMaxQuery(ScoreMode.Max, 0, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Max, 1, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Max, 2, null);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(2f));
response = minMaxQuery(ScoreMode.Max, 3, null);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
response = minMaxQuery(ScoreMode.Max, 4, null);
assertThat(response.getHits().getTotalHits(), equalTo(0L));
response = minMaxQuery(ScoreMode.Max, 0, 4);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Max, 0, 3);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Max, 0, 2);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Max, 2, 2);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Max, 3, 2));
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
// Score mode = AVG
response = minMaxQuery(ScoreMode.Avg, 0, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1.5f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Avg, 1, null);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1.5f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Avg, 2, null);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1.5f));
response = minMaxQuery(ScoreMode.Avg, 3, null);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
response = minMaxQuery(ScoreMode.Avg, 4, null);
assertThat(response.getHits().getTotalHits(), equalTo(0L));
response = minMaxQuery(ScoreMode.Avg, 0, 4);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1.5f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Avg, 0, 3);
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("4"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(2f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1.5f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Avg, 0, 2);
assertThat(response.getHits().getTotalHits(), equalTo(2L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1.5f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(1f));
response = minMaxQuery(ScoreMode.Avg, 2, 2);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(1.5f));
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Avg, 3, 2));
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testScoreForParentChildQueriesWithFunctionScore.
public void testScoreForParentChildQueriesWithFunctionScore() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent").addMapping("child1", "_parent", "type=parent"));
ensureGreen();
indexRandom(true, createDocBuilders().toArray(new IndexRequestBuilder[0]));
SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")).boostMode(CombineFunction.REPLACE), ScoreMode.Total)).get();
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("1"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(6f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(4f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(3f));
response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")).boostMode(CombineFunction.REPLACE), ScoreMode.Max)).get();
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(4f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("1"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(2f));
response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")).boostMode(CombineFunction.REPLACE), ScoreMode.Avg)).get();
assertThat(response.getHits().getTotalHits(), equalTo(3L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("3"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(4f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(3f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("1"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(1.5f));
response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), fieldValueFactorFunction("p_field2")).boostMode(CombineFunction.REPLACE), true)).addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get();
assertThat(response.getHits().getTotalHits(), equalTo(7L));
assertThat(response.getHits().getHits()[0].getId(), equalTo("13"));
assertThat(response.getHits().getHits()[0].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[1].getId(), equalTo("14"));
assertThat(response.getHits().getHits()[1].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[2].getId(), equalTo("15"));
assertThat(response.getHits().getHits()[2].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[3].getId(), equalTo("16"));
assertThat(response.getHits().getHits()[3].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[4].getId(), equalTo("17"));
assertThat(response.getHits().getHits()[4].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[5].getId(), equalTo("18"));
assertThat(response.getHits().getHits()[5].getScore(), equalTo(5f));
assertThat(response.getHits().getHits()[6].getId(), equalTo("1"));
assertThat(response.getHits().getHits()[6].getScore(), equalTo(5f));
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class DecayFunctionScoreIT method testMultiFieldOptions.
public void testMultiFieldOptions() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().startObject("num").field("type", "float").endObject().endObject().endObject().endObject()));
// Index for testing MIN and MAX
IndexRequestBuilder doc1 = client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startArray("loc").startObject().field("lat", 10).field("lon", 20).endObject().startObject().field("lat", 12).field("lon", 23).endObject().endArray().endObject());
IndexRequestBuilder doc2 = client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 11).field("lon", 22).endObject().endObject());
indexRandom(true, doc1, doc2);
ActionFuture<SearchResponse> response = client().search(searchRequest().source(searchSource().query(baseQuery)));
SearchResponse sr = response.actionGet();
assertSearchHits(sr, "1", "2");
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2)));
List<Float> lonlat = new ArrayList<>();
lonlat.add(20f);
lonlat.add(10f);
response = client().search(searchRequest().source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", lonlat, "1000km").setMultiValueMode(MultiValueMode.MIN)))));
sr = response.actionGet();
assertSearchHits(sr, "1", "2");
sh = sr.getHits();
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat(sh.getAt(1).getId(), equalTo("2"));
response = client().search(searchRequest().source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", lonlat, "1000km").setMultiValueMode(MultiValueMode.MAX)))));
sr = response.actionGet();
assertSearchHits(sr, "1", "2");
sh = sr.getHits();
assertThat(sh.getAt(0).getId(), equalTo("2"));
assertThat(sh.getAt(1).getId(), equalTo("1"));
// Now test AVG and SUM
doc1 = client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startArray("num").value(0.0).value(1.0).value(2.0).endArray().endObject());
doc2 = client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 1.0).endObject());
indexRandom(true, doc1, doc2);
response = client().search(searchRequest().source(searchSource().query(functionScoreQuery(baseQuery, linearDecayFunction("num", "0", "10").setMultiValueMode(MultiValueMode.SUM)))));
sr = response.actionGet();
assertSearchHits(sr, "1", "2");
sh = sr.getHits();
assertThat(sh.getAt(0).getId(), equalTo("2"));
assertThat(sh.getAt(1).getId(), equalTo("1"));
assertThat(1.0 - sh.getAt(0).getScore(), closeTo((1.0 - sh.getAt(1).getScore()) / 3.0, 1.e-6d));
response = client().search(searchRequest().source(searchSource().query(functionScoreQuery(baseQuery, linearDecayFunction("num", "0", "10").setMultiValueMode(MultiValueMode.AVG)))));
sr = response.actionGet();
assertSearchHits(sr, "1", "2");
sh = sr.getHits();
assertThat((double) (sh.getAt(0).getScore()), closeTo((sh.getAt(1).getScore()), 1.e-6d));
}
Aggregations