Search in sources :

Example 6 with HasParentQueryBuilder

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

the class ChildQuerySearchIT method testHighlightersIgnoreParentChild.

public void testHighlightersIgnoreParentChild() {
    assertAcked(prepareCreate("test").addMapping("parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets").addMapping("child-type", "_parent", "type=parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets"));
    client().prepareIndex("test", "parent-type", "parent-id").setSource("searchText", "quick brown fox").get();
    client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("searchText", "quick brown fox").get();
    refresh();
    String[] highlightTypes = new String[] { "plain", "fvh", "postings" };
    for (String highlightType : highlightTypes) {
        logger.info("Testing with highlight type [{}]", highlightType);
        SearchResponse searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
        assertHitCount(searchResponse, 1);
        assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id"));
        HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
        assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
        searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
        assertHitCount(searchResponse, 1);
        assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id"));
        highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
        assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Matchers.containsString(org.hamcrest.Matchers.containsString) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)

Example 7 with HasParentQueryBuilder

use of org.elasticsearch.index.query.HasParentQueryBuilder in project sonarqube by SonarSource.

the class AuthorizationTypeSupportTest method createQueryFilter_sets_filter_on_anyone_and_user_id_if_user_is_logged_in_but_has_no_groups.

@Test
public void createQueryFilter_sets_filter_on_anyone_and_user_id_if_user_is_logged_in_but_has_no_groups() {
    userSession.logIn().setUserId(1234);
    HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter();
    assertJson(filter.toString()).isSimilarTo("{" + "  \"has_parent\": {" + "    \"query\": {" + "      \"bool\": {" + "        \"filter\": {" + "          \"bool\": {" + "            \"should\": [" + "              {" + "                \"term\": {" + "                  \"allowAnyone\": true" + "                }" + "              }," + "              {" + "                \"term\": {" + "                  \"userIds\": 1234" + "                }" + "              }" + "            ]" + "          }" + "        }" + "      }" + "    }," + "    \"parent_type\": \"authorization\"" + "  }" + "}");
}
Also used : HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) Test(org.junit.Test)

Aggregations

HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)7 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)4 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 Test (org.junit.Test)3 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)2 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)2 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)2 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)2 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 RandomScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder)1 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)1 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1