Search in sources :

Example 1 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class CCSDuelIT method testHasChildWithInnerHit.

public void testHasChildWithInnerHit() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("creationDate").gte("2019/01/01").lte("2019/01/31");
    HasChildQueryBuilder query = new HasChildQueryBuilder("answer", rangeQueryBuilder, ScoreMode.Total);
    query.innerHit(new InnerHitBuilder("inner"));
    sourceBuilder.query(query);
    searchRequest.source(sourceBuilder);
    duelSearch(searchRequest, CCSDuelIT::assertHits);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder)

Example 2 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class InnerHitsIT method testNestedInnerHitWrappedInParentChildInnerhit.

public void testNestedInnerHitWrappedInParentChildInnerhit() {
    assertAcked(prepareCreate("test").addMapping("doc", addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), "nested_type", "nested")));
    createIndexRequest("test", "parent_type", "1", null, "key", "value").get();
    createIndexRequest("test", "child_type", "2", "1", "nested_type", Collections.singletonMap("key", "value")).get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(boolQuery().must(matchQuery("key", "value")).should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder()), ScoreMode.None).innerHit(new InnerHitBuilder()))).get();
    assertHitCount(response, 1);
    SearchHit hit = response.getHits().getAt(0);
    String parentId = (String) extractValue("join_field.parent", hit.getInnerHits().get("child_type").getAt(0).getSourceAsMap());
    assertThat(parentId, equalTo("1"));
    assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue());
}
Also used : SearchHit(org.opensearch.search.SearchHit) OpenSearchAssertions.assertSearchHit(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class InnerHitsIT method testParentChildMultipleLayers.

public void testParentChildMultipleLayers() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("doc", addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "article", "comment", "comment", "remark"), "title", "text", "message", "text")));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(createIndexRequest("articles", "article", "1", null, "title", "quick brown fox"));
    requests.add(createIndexRequest("articles", "comment", "3", "1", "message", "fox eat quick"));
    requests.add(createIndexRequest("articles", "remark", "5", "3", "message", "good").setRouting("1"));
    requests.add(createIndexRequest("articles", "article", "2", null, "title", "big gray elephant"));
    requests.add(createIndexRequest("articles", "comment", "4", "2", "message", "elephant captured"));
    requests.add(createIndexRequest("articles", "remark", "6", "4", "message", "bad").setRouting("2"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None).innerHit(new InnerHitBuilder()), ScoreMode.None).innerHit(new InnerHitBuilder())).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("3"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("5"));
    response = client().prepareSearch("articles").setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None).innerHit(new InnerHitBuilder()), ScoreMode.None).innerHit(new InnerHitBuilder())).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("4"));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("6"));
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) OpenSearchAssertions.assertSearchHits(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class InnerHitsIT method testRoyals.

public void testRoyals() throws Exception {
    assertAcked(prepareCreate("royals").addMapping("doc", buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "king", "prince", "prince", "duke", "duke", "earl", "earl", "baron")));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(createIndexRequest("royals", "king", "king", null));
    requests.add(createIndexRequest("royals", "prince", "prince", "king"));
    requests.add(createIndexRequest("royals", "duke", "duke", "prince").setRouting("king"));
    requests.add(createIndexRequest("royals", "earl", "earl1", "duke").setRouting("king"));
    requests.add(createIndexRequest("royals", "earl", "earl2", "duke").setRouting("king"));
    requests.add(createIndexRequest("royals", "earl", "earl3", "duke").setRouting("king"));
    requests.add(createIndexRequest("royals", "earl", "earl4", "duke").setRouting("king"));
    requests.add(createIndexRequest("royals", "baron", "baron1", "earl1").setRouting("king"));
    requests.add(createIndexRequest("royals", "baron", "baron2", "earl2").setRouting("king"));
    requests.add(createIndexRequest("royals", "baron", "baron3", "earl3").setRouting("king"));
    requests.add(createIndexRequest("royals", "baron", "baron4", "earl4").setRouting("king"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("royals").setQuery(boolQuery().filter(hasParentQuery("prince", hasParentQuery("king", matchAllQuery(), false).innerHit(new InnerHitBuilder().setName("kings")), false).innerHit(new InnerHitBuilder().setName("princes"))).filter(hasChildQuery("earl", hasChildQuery("baron", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("barons")), ScoreMode.None).innerHit(new InnerHitBuilder().addSort(SortBuilders.fieldSort("id").order(SortOrder.ASC)).setName("earls").setSize(4)))).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("duke"));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("earls");
    assertThat(innerHits.getTotalHits().value, 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().value, equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron1"));
    innerInnerHits = innerHits.getAt(1).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron2"));
    innerInnerHits = innerHits.getAt(2).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron3"));
    innerInnerHits = innerHits.getAt(3).getInnerHits().get("barons");
    assertThat(innerInnerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("baron4"));
    innerHits = response.getHits().getAt(0).getInnerHits().get("princes");
    assertThat(innerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerHits.getAt(0).getId(), equalTo("prince"));
    innerInnerHits = innerHits.getAt(0).getInnerHits().get("kings");
    assertThat(innerInnerHits.getTotalHits().value, equalTo(1L));
    assertThat(innerInnerHits.getAt(0).getId(), equalTo("king"));
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) OpenSearchAssertions.assertSearchHits(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class InnerHitsIT method testInnerHitsOnHasParent.

public void testInnerHitsOnHasParent() throws Exception {
    assertAcked(prepareCreate("stack").addMapping("doc", addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "question", "answer"), "body", "text")));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(createIndexRequest("stack", "question", "1", null, "body", "I'm using HTTPS + Basic authentication " + "to protect a resource. How can I throttle authentication attempts to protect against brute force attacks?"));
    requests.add(createIndexRequest("stack", "answer", "3", "1", "body", "install fail2ban and enable rules for apache"));
    requests.add(createIndexRequest("stack", "question", "2", null, "body", "I have firewall rules set up and also denyhosts installed.\\ndo I also need to install fail2ban?"));
    requests.add(createIndexRequest("stack", "answer", "4", "2", "body", "Denyhosts protects only ssh; Fail2Ban protects all daemons."));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("stack").addSort("id", SortOrder.ASC).setQuery(boolQuery().must(matchQuery("body", "fail2ban")).must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder()))).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    SearchHit searchHit = response.getHits().getAt(0);
    assertThat(searchHit.getId(), equalTo("3"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits().value, equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("1"));
    searchHit = response.getHits().getAt(1);
    assertThat(searchHit.getId(), equalTo("4"));
    assertThat(searchHit.getInnerHits().get("question").getTotalHits().value, equalTo(1L));
    assertThat(searchHit.getInnerHits().get("question").getAt(0).getId(), equalTo("2"));
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchHit(org.opensearch.search.SearchHit) OpenSearchAssertions.assertSearchHit(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)43 SearchResponse (org.opensearch.action.search.SearchResponse)22 ArrayList (java.util.ArrayList)15 SearchHits (org.opensearch.search.SearchHits)14 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)13 QueryBuilder (org.opensearch.index.query.QueryBuilder)11 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)9 SearchHit (org.opensearch.search.SearchHit)9 OpenSearchAssertions.assertSearchHits (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)7 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)6 CollapseBuilder (org.opensearch.search.collapse.CollapseBuilder)5 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)5 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)5 OpenSearchAssertions.assertSearchHit (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit)5 TotalHits (org.apache.lucene.search.TotalHits)4 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)3 WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)3