Search in sources :

Example 16 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testNestedDefinedAsObject.

// Issue #9723
public void testNestedDefinedAsObject() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", "comments", "type=nested", "title", "type=text"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startObject("comments").field("message", "fox eat quick").endObject().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getChild(), nullValue());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 17 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testRoyals.

public void testRoyals() throws Exception {
    assertAcked(prepareCreate("royals").addMapping("king").addMapping("prince", "_parent", "type=king").addMapping("duke", "_parent", "type=prince").addMapping("earl", "_parent", "type=duke").addMapping("baron", "_parent", "type=earl"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("royals", "king", "king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "prince", "prince").setParent("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "duke", "duke").setParent("prince").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "earl", "earl1").setParent("duke").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "earl", "earl2").setParent("duke").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "earl", "earl3").setParent("duke").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "earl", "earl4").setParent("duke").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "baron", "baron1").setParent("earl1").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "baron", "baron2").setParent("earl2").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "baron", "baron3").setParent("earl3").setRouting("king").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("royals", "baron", "baron4").setParent("earl4").setRouting("king").setSource("{}", XContentType.JSON));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("royals").setTypes("duke").setQuery(boolQuery().filter(hasParentQuery("prince", hasParentQuery("king", matchAllQuery(), false).innerHit(new InnerHitBuilder().setName("kings"), false), false).innerHit(new InnerHitBuilder().setName("princes"), false)).filter(hasChildQuery("earl", hasChildQuery("baron", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("barons"), false), ScoreMode.None).innerHit(new InnerHitBuilder().addSort(SortBuilders.fieldSort("_uid").order(SortOrder.ASC)).setName("earls").setSize(4), false))).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("duke"));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("earls");
    assertThat(innerHits.getTotalHits(), equalTo(4L));
    assertThat(innerHits.getAt(0).getId(), equalTo("earl1"));
    assertThat(innerHits.getAt(1).getId(), equalTo("earl2"));
    assertThat(innerHits.getAt(2).getId(), equalTo("earl3"));
    assertThat(innerHits.getAt(3).getId(), equalTo("earl4"));
    SearchHits innerInnerHits = innerHits.getAt(0).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits(), equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron1"));
    innerInnerHits = innerHits.getAt(1).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits(), equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron2"));
    innerInnerHits = innerHits.getAt(2).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits(), equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron3"));
    innerInnerHits = innerHits.getAt(3).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits(), equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron4"));
    innerHits = response.getHits().getAt(0).getInnerHits().get("princes");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("prince"));
    innerInnerHits = innerHits.getAt(0).getInnerHits().get("kings");
    assertThat(innerInnerHits.getTotalHits(), equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("king"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 18 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testMatchesQueriesParentChildInnerHits.

public void testMatchesQueriesParentChildInnerHits() throws Exception {
    assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("index", "parent", "1").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index", "child", "1").setParent("1").setSource("field", "value1"));
    requests.add(client().prepareIndex("index", "child", "2").setParent("1").setSource("field", "value2"));
    requests.add(client().prepareIndex("index", "parent", "2").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index", "child", "3").setParent("2").setSource("field", "value1"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("index").setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(response, 2);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
    assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
    QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None).innerHit(new InnerHitBuilder(), false);
    response = client().prepareSearch("index").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name2"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 19 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testMatchesQueriesNestedInnerHits.

public void testMatchesQueriesNestedInnerHits() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", builder));
    ensureGreen();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = randomIntBetween(2, 35);
    requests.add(client().prepareIndex("test", "type1", "0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
    for (int i = 2; i < numDocs; i++) {
        requests.add(client().prepareIndex("test", "type1", String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    }
    indexRandom(true, requests);
    waitForRelocation(ClusterHealthStatus.GREEN);
    QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
    query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)), false);
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertAllSuccessful(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo((long) numDocs));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
    for (int i = 2; i < numDocs; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with InnerHitBuilder

use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testInnerHitsWithObjectFieldThatHasANestedField.

public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("properties").startObject("comments").field("type", "object").startObject("properties").startObject("messages").field("type", "nested").endObject().endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startObject("comments").startArray("messages").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "bear eat quick").endObject().endArray().endObject().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    SearchHit hit = response.getHits().getAt(0);
    assertThat(hit.getId(), equalTo("1"));
    SearchHits messages = hit.getInnerHits().get("comments.messages");
    assertThat(messages.getTotalHits(), equalTo(1L));
    assertThat(messages.getAt(0).getId(), equalTo("1"));
    assertThat(messages.getAt(0).getNestedIdentity().getField().string(), equalTo("comments.messages"));
    assertThat(messages.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(messages.getAt(0).getNestedIdentity().getChild(), nullValue());
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "bear"), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    hit = response.getHits().getAt(0);
    assertThat(hit.getId(), equalTo("1"));
    messages = hit.getInnerHits().get("comments.messages");
    assertThat(messages.getTotalHits(), equalTo(1L));
    assertThat(messages.getAt(0).getId(), equalTo("1"));
    assertThat(messages.getAt(0).getNestedIdentity().getField().string(), equalTo("comments.messages"));
    assertThat(messages.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(messages.getAt(0).getNestedIdentity().getChild(), nullValue());
    // index the message in an object form instead of an array
    requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startObject("comments").startObject("messages").field("message", "fox eat quick").endObject().endObject().endObject()));
    indexRandom(true, requests);
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    hit = response.getHits().getAt(0);
    ;
    assertThat(hit.getId(), equalTo("1"));
    messages = hit.getInnerHits().get("comments.messages");
    assertThat(messages.getTotalHits(), equalTo(1L));
    assertThat(messages.getAt(0).getId(), equalTo("1"));
    assertThat(messages.getAt(0).getNestedIdentity().getField().string(), equalTo("comments.messages"));
    assertThat(messages.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(messages.getAt(0).getNestedIdentity().getChild(), nullValue());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)22 SearchResponse (org.elasticsearch.action.search.SearchResponse)17 ArrayList (java.util.ArrayList)13 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)13 SearchHits (org.elasticsearch.search.SearchHits)10 SearchHit (org.elasticsearch.search.SearchHit)8 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)6 ElasticsearchAssertions.assertSearchHit (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)4 IOException (java.io.IOException)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Text (org.elasticsearch.common.text.Text)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 Script (org.elasticsearch.script.Script)2 SearchHitField (org.elasticsearch.search.SearchHitField)2 CollapseBuilder (org.elasticsearch.search.collapse.CollapseBuilder)2 InternalSearchResponse (org.elasticsearch.search.internal.InternalSearchResponse)2