private SearchResponse minMaxQuery(ScoreMode scoreMode, int minChildren, Integer maxChildren) throws SearchPhaseExecutionException { HasChildQueryBuilder hasChildQuery = hasChildQuery( "child", QueryBuilders.functionScoreQuery(constantScoreQuery(QueryBuilders.termQuery("foo", "two")), new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1)) }).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM), scoreMode) .minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN); return client() .prepareSearch("test") .setQuery(hasChildQuery) .addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).get(); }
public void testUnsupportedQueries() { RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D"); RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now"); PercolatorFieldMapper.verifyQuery(rangeQuery1); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(rangeQuery2)); PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery1)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(rangeQuery2))); PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1))); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(rangeQuery2))); PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder())); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery2, new MatchAllQueryBuilder()))); PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder())); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery2, new RandomScoreFunctionBuilder()))); HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery))); }
@Override public Scores classSearch(ModifiableSearchParams searchParams, ModifiableRankParams rankParams) { searchParams.model(BuiltInModel.FACT); if (rankParams instanceof ModifiableIndraParams) { configureDistributionalParams(searchParams.getKbId(), (ModifiableIndraParams) rankParams); } QueryBuilder queryBuilder = boolQuery() .must(nestedQuery("p", termQuery("p.id", "is-a"), ScoreMode.Max)) .should(nestedQuery("o", matchQuery("o.value", searchParams.getSearchTerm()), ScoreMode.Max)) .minimumShouldMatch("1"); Searcher searcher = core.getSearcher(searchParams.getKbId()); Scores scores = searcher.search(new ElasticQueryHolder(queryBuilder, searchParams)); List<Score> classes2Score = scores.stream() .map(s -> new Score(((Fact)s.getEntry()).getObject(), s.getValue())).collect(Collectors.toList()); return Rankers.apply(new Scores(classes2Score), rankParams, searchParams.getSearchTerm()); }
@Override public Scores propertySearch(ModifiableSearchParams searchParams, ModifiableRankParams rankParams) { searchParams.model(BuiltInModel.PROPERTY); if (rankParams instanceof ModifiableIndraParams) { configureDistributionalParams(searchParams.getKbId(), (ModifiableIndraParams) rankParams); } QueryBuilder queryBuilder = boolQuery() .should(nestedQuery("hyponyms", matchQuery("hyponyms.word", searchParams.getSearchTerm()), ScoreMode.Max)) .should(nestedQuery("hypernyms", matchQuery("hypernyms.word", searchParams.getSearchTerm()), ScoreMode.Max)) .should(nestedQuery("synonyms", matchQuery("synonyms.word", searchParams.getSearchTerm()), ScoreMode.Max)) .minimumNumberShouldMatch(1); Searcher searcher = core.getSearcher(searchParams.getKbId()); Scores scores = searcher.search(new ElasticQueryHolder(queryBuilder, searchParams)); return Rankers.apply(scores, rankParams, searchParams.getSearchTerm()); }
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()); }
public void testNestedInnerHitWrappedInParentChildInnerhit() throws Exception { assertAcked(prepareCreate("test").addMapping("child_type", "_parent", "type=parent_type", "nested_type", "type=nested")); client().prepareIndex("test", "parent_type", "1").setSource("key", "value").get(); client().prepareIndex("test", "child_type", "2").setParent("1").setSource("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(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false))) .get(); assertHitCount(response, 1); SearchHit hit = response.getHits().getAt(0); assertThat(hit.getInnerHits().get("child_type").getAt(0).field("_parent").getValue(), equalTo("1")); assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue()); }
public void test2744() throws IOException { assertAcked(prepareCreate("test") .addMapping("foo") .addMapping("test", "_parent", "type=foo")); ensureGreen(); // index simple data client().prepareIndex("test", "foo", "1").setSource("foo", 1).get(); client().prepareIndex("test", "test").setSource("foo", 1).setParent("1").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test"). setQuery(hasChildQuery("test", matchQuery("foo", 1), ScoreMode.None)) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); }
public void testDfsSearchType() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); // index simple data client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").get(); client().prepareIndex("test", "child", "c1").setSource("c_field", "red").setParent("p1").get(); client().prepareIndex("test", "child", "c2").setSource("c_field", "yellow").setParent("p1").get(); client().prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").get(); client().prepareIndex("test", "child", "c3").setSource("c_field", "blue").setParent("p2").get(); client().prepareIndex("test", "child", "c4").setSource("c_field", "red").setParent("p2").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(boolQuery().mustNot(hasChildQuery("child", boolQuery().should(queryStringQuery("c_field:*")), ScoreMode.None))) .get(); assertNoFailures(searchResponse); searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(boolQuery().mustNot(hasParentQuery("parent", boolQuery().should(queryStringQuery("p_field:*")), false))).execute() .actionGet(); assertNoFailures(searchResponse); }
public void testHasChildAndHasParentFailWhenSomeSegmentsDontContainAnyParentOrChildDocs() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get(); client().prepareIndex("test", "child", "1").setParent("1").setSource("c_field", 1).get(); client().admin().indices().prepareFlush("test").get(); client().prepareIndex("test", "type1", "1").setSource("p_field", 1).get(); client().admin().indices().prepareFlush("test").get(); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test") .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); }
public void testHasChildAndHasParentFilter_withFilter() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get(); client().prepareIndex("test", "child", "2").setParent("1").setSource("c_field", 1).get(); client().admin().indices().prepareFlush("test").get(); client().prepareIndex("test", "type1", "3").setSource("p_field", 2).get(); client().admin().indices().prepareFlush("test").get(); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", termQuery("c_field", 1), ScoreMode.None))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); searchResponse = client().prepareSearch("test") .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", 1), false))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); }
public void testHasChildInnerHitsHighlighting() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get(); client().prepareIndex("test", "child", "2").setParent("1").setSource("c_field", "foo bar").get(); client().admin().indices().prepareFlush("test").get(); SearchResponse searchResponse = client().prepareSearch("test").setQuery( hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None) .innerHit(new InnerHitBuilder().setHighlightBuilder( new HighlightBuilder().field(new Field("c_field") .highlightQuery(QueryBuilders.matchQuery("c_field", "bar")))), false)) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); SearchHit[] searchHits = searchResponse.getHits().getHits()[0].getInnerHits().get("child").getHits(); assertThat(searchHits.length, equalTo(1)); assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments().length, equalTo(1)); assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments()[0].string(), equalTo("foo <em>bar</em>")); }
public void testQueryBeforeChildType() throws Exception { assertAcked(prepareCreate("test") .addMapping("features") .addMapping("posts", "_parent", "type=features") .addMapping("specials")); ensureGreen(); client().prepareIndex("test", "features", "1").setSource("field", "foo").get(); client().prepareIndex("test", "posts", "1").setParent("1").setSource("field", "bar").get(); refresh(); SearchResponse resp; resp = client().prepareSearch("test") .setSource(new SearchSourceBuilder().query(QueryBuilders.hasChildQuery("posts", QueryBuilders.matchQuery("field", "bar"), ScoreMode.None))) .get(); assertHitCount(resp, 1L); }
/** * @return a {@link HasChildQueryBuilder} with random values all over the place */ @Override protected HasChildQueryBuilder doCreateTestQueryBuilder() { int min = randomIntBetween(0, Integer.MAX_VALUE / 2); int max = randomIntBetween(min, Integer.MAX_VALUE); QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random()); if (randomBoolean()) { requiresRewrite = true; innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString()); } HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_TYPE, innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values())); hqb.minMaxChildren(min, max); hqb.ignoreUnmapped(randomBoolean()); if (randomBoolean()) { hqb.innerHit(new InnerHitBuilder() .setName(randomAsciiOfLengthBetween(1, 10)) .setSize(randomIntBetween(0, 100)) .addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)), hqb.ignoreUnmapped()); } return hqb; }
public void testIllegalValues() { QueryBuilder query = RandomQueryBuilder.createQuery(random()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery(null, query, ScoreMode.None)); assertEquals("[has_child] requires 'type' field", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", null, ScoreMode.None)); assertEquals("[has_child] requires 'query' field", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", query, null)); assertEquals("[has_child] requires 'score_mode' field", e.getMessage()); int positiveValue = randomIntBetween(0, Integer.MAX_VALUE); HasChildQueryBuilder foo = QueryBuilders.hasChildQuery("foo", query, ScoreMode.None); // all good e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(randomIntBetween(Integer.MIN_VALUE, -1), positiveValue)); assertEquals("[has_child] requires non-negative 'min_children' field", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, randomIntBetween(Integer.MIN_VALUE, -1))); assertEquals("[has_child] requires non-negative 'max_children' field", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, positiveValue - 10)); assertEquals("[has_child] 'max_children' is less than 'min_children'", e.getMessage()); }
/** * 增加嵌套插 * * @param boolQuery * @param where * @param subQuery */ private void addSubQuery(BoolQueryBuilder boolQuery, Where where, QueryBuilder subQuery) { if(where instanceof Condition){ Condition condition = (Condition) where; if(condition.isNested()){ subQuery = QueryBuilders.nestedQuery(condition.getNestedPath(), subQuery, ScoreMode.None); } else if(condition.isChildren()) { subQuery = QueryBuilders.hasChildQuery(condition.getChildType(), subQuery, ScoreMode.None); } } if (where.getConn() == CONN.AND) { boolQuery.must(subQuery); } else { boolQuery.should(subQuery); } }
/** * @return a {@link HasChildQueryBuilder} with random values all over the place */ @Override protected NestedQueryBuilder doCreateTestQueryBuilder() { QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random()); if (randomBoolean()) { requiresRewrite = true; innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString()); } NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values())); nqb.ignoreUnmapped(randomBoolean()); if (randomBoolean()) { nqb.innerHit(new InnerHitBuilder() .setName(randomAsciiOfLengthBetween(1, 10)) .setSize(randomIntBetween(0, 100)) .addSort(new FieldSortBuilder(INT_FIELD_NAME).order(SortOrder.ASC)), nqb.ignoreUnmapped()); } return nqb; }
public void testInlineLeafInnerHitsNestedQueryViaDisMaxQuery() { InnerHitBuilder leafInnerHits1 = randomInnerHits(); NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None) .innerHit(leafInnerHits1, false); InnerHitBuilder leafInnerHits2 = randomInnerHits(); HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder("type", new MatchAllQueryBuilder(), ScoreMode.None) .innerHit(leafInnerHits2, false); DisMaxQueryBuilder disMaxQueryBuilder = new DisMaxQueryBuilder(); disMaxQueryBuilder.add(nestedQueryBuilder); disMaxQueryBuilder.add(hasChildQueryBuilder); Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>(); disMaxQueryBuilder.extractInnerHitBuilders(innerHitBuilders); assertThat(innerHitBuilders.size(), equalTo(2)); assertThat(innerHitBuilders.get(leafInnerHits1.getName()), notNullValue()); assertThat(innerHitBuilders.get(leafInnerHits2.getName()), notNullValue()); }
public void testBuild_ingoreUnmappedNestQuery() throws Exception { QueryShardContext queryShardContext = mock(QueryShardContext.class); when(queryShardContext.getObjectMapper("path")).thenReturn(null); SearchContext searchContext = mock(SearchContext.class); when(searchContext.getQueryShardContext()).thenReturn(queryShardContext); InnerHitBuilder leafInnerHits = randomInnerHits(); NestedQueryBuilder query1 = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); query1.innerHit(leafInnerHits, false); expectThrows(IllegalStateException.class, () -> query1.innerHit().build(searchContext, new InnerHitsContext())); NestedQueryBuilder query2 = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); query2.innerHit(leafInnerHits, true); InnerHitsContext innerHitsContext = new InnerHitsContext(); query2.innerHit().build(searchContext, innerHitsContext); assertThat(innerHitsContext.getInnerHits().size(), equalTo(0)); }
public void testBuild_ignoreUnmappedHasChildQuery() throws Exception { QueryShardContext queryShardContext = mock(QueryShardContext.class); when(queryShardContext.documentMapper("type")).thenReturn(null); SearchContext searchContext = mock(SearchContext.class); when(searchContext.getQueryShardContext()).thenReturn(queryShardContext); InnerHitBuilder leafInnerHits = randomInnerHits(); HasChildQueryBuilder query1 = new HasChildQueryBuilder("type", new MatchAllQueryBuilder(), ScoreMode.None) .innerHit(leafInnerHits, false); expectThrows(IllegalStateException.class, () -> query1.innerHit().build(searchContext, new InnerHitsContext())); HasChildQueryBuilder query2 = new HasChildQueryBuilder("type", new MatchAllQueryBuilder(), ScoreMode.None) .innerHit(leafInnerHits, true); InnerHitsContext innerHitsContext = new InnerHitsContext(); query2.innerHit().build(searchContext, innerHitsContext); assertThat(innerHitsContext.getInnerHits().size(), equalTo(0)); }
@Override protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; Query childFilter = Queries.not(parentFilter); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); TopDocs topDocs = searcher.search(query, 5, sort); assertThat(topDocs.totalHits, equalTo(7)); assertThat(topDocs.scoreDocs.length, equalTo(5)); assertThat(topDocs.scoreDocs[0].doc, equalTo(11)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[1].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[3].doc, equalTo(15)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[4].doc, equalTo(19)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3)); }
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher, IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; Query childFilter = Queries.not(parentFilter); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); TopDocs topDocs = searcher.search(query, 5, sort); assertThat(topDocs.totalHits, equalTo(7)); assertThat(topDocs.scoreDocs.length, equalTo(5)); assertThat(topDocs.scoreDocs[0].doc, equalTo(11)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[1].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[3].doc, equalTo(15)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[4].doc, equalTo(19)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3)); }
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException { MultiValueMode sortMode = MultiValueMode.AVG; Query childFilter = Queries.not(parentFilter); XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter)); Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("field2", nestedComparatorSource)); TopDocs topDocs = searcher.search(query, 5, sort); assertThat(topDocs.totalHits, equalTo(7)); assertThat(topDocs.scoreDocs.length, equalTo(5)); assertThat(topDocs.scoreDocs[0].doc, equalTo(11)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[1].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[2].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[3].doc, equalTo(15)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[4].doc, equalTo(19)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4)); }
public void testAliasesFilterWithHasChildQuery() throws Exception { assertAcked(prepareCreate("my-index") .addMapping("parent") .addMapping("child", "_parent", "type=parent") ); client().prepareIndex("my-index", "parent", "1").setSource("{}", XContentType.JSON).get(); client().prepareIndex("my-index", "child", "2").setSource("{}", XContentType.JSON).setParent("1").get(); refresh(); assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter1", hasChildQuery("child", matchAllQuery(), ScoreMode.None))); assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", hasParentQuery("parent", matchAllQuery(), false))); SearchResponse response = client().prepareSearch("filter1").get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); response = client().prepareSearch("filter2").get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("2")); }
@Override public Scores pivotedSearch(InstanceEntity pivot, ModifiableSearchParams searchParams, ModifiableRankParams rankParams) { searchParams.model(BuiltInModel.FACT); if (rankParams instanceof ModifiableIndraParams) { configureDistributionalParams(searchParams.getKbId(), (ModifiableIndraParams) rankParams); } QueryBuilder queryBuilder = boolQuery() .should(nestedQuery("s", termQuery("s.id", pivot.getId()), ScoreMode.Max)) .should(nestedQuery("o", termQuery("o.id", pivot.getId()), ScoreMode.Max)).minimumNumberShouldMatch(1); Searcher searcher = core.getSearcher(searchParams.getKbId()); Scores scores = searcher.search(new ElasticQueryHolder(queryBuilder, searchParams)); // We have to remap the facts to properties, the real target of the ranker call. // Thus we're discarding the score values from the underlying search engine. Shall we? Scores propScores = new Scores(scores.stream() .map(s -> ((Fact) s.getEntry()).getPredicate()) .distinct() .map(p -> new Score(p, 0)) .collect(Collectors.toList())); return Rankers.apply(propScores, rankParams, searchParams.getSearchTerm()); }
/** * Read from a stream. */ public NestedQueryBuilder(StreamInput in) throws IOException { super(in); path = in.readString(); scoreMode = ScoreMode.values()[in.readVInt()]; query = in.readNamedWriteable(QueryBuilder.class); innerHitBuilder = in.readOptionalWriteable(InnerHitBuilder::new); ignoreUnmapped = in.readBoolean(); }
private HasChildQueryBuilder(String type, QueryBuilder query, int minChildren, int maxChildren, ScoreMode scoreMode, InnerHitBuilder innerHitBuilder) { this.type = requireValue(type, "[" + NAME + "] requires 'type' field"); this.query = requireValue(query, "[" + NAME + "] requires 'query' field"); this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field"); this.innerHitBuilder = innerHitBuilder; this.minChildren = minChildren; this.maxChildren = maxChildren; }
/** * Read from a stream. */ public HasChildQueryBuilder(StreamInput in) throws IOException { super(in); type = in.readString(); minChildren = in.readInt(); maxChildren = in.readInt(); scoreMode = ScoreMode.values()[in.readVInt()]; query = in.readNamedWriteable(QueryBuilder.class); innerHitBuilder = in.readOptionalWriteable(InnerHitBuilder::new); ignoreUnmapped = in.readBoolean(); }
public static ScoreMode parseScoreMode(String scoreModeString) { if ("none".equals(scoreModeString)) { return ScoreMode.None; } else if ("min".equals(scoreModeString)) { return ScoreMode.Min; } else if ("max".equals(scoreModeString)) { return ScoreMode.Max; } else if ("avg".equals(scoreModeString)) { return ScoreMode.Avg; } else if ("sum".equals(scoreModeString)) { return ScoreMode.Total; } throw new IllegalArgumentException("No score mode for child query [" + scoreModeString + "] found"); }
public static String scoreModeAsString(ScoreMode scoreMode) { if (scoreMode == ScoreMode.Total) { // Lucene uses 'total' but 'sum' is more consistent with other elasticsearch APIs return "sum"; } else { return scoreMode.name().toLowerCase(Locale.ROOT); } }
LateParsingQuery(Query toQuery, Query innerQuery, int minChildren, int maxChildren, String parentType, ScoreMode scoreMode, ParentChildIndexFieldData parentChildIndexFieldData, Similarity similarity) { this.toQuery = toQuery; this.innerQuery = innerQuery; this.minChildren = minChildren; this.maxChildren = maxChildren; this.parentType = parentType; this.scoreMode = scoreMode; this.parentChildIndexFieldData = parentChildIndexFieldData; this.similarity = similarity; }
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")); }
public void testDontExplode() throws Exception { assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent")); List<IndexRequestBuilder> requests = new ArrayList<>(); requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}", XContentType.JSON)); requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1")); indexRandom(true, requests); QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None) .innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false); SearchResponse response = client().prepareSearch("index1") .setQuery(query) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); assertHitCount(response, 1); assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested")); client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject() .startArray("nested") .startObject() .field("field", "value1") .endObject() .endArray() .endObject()) .setRefreshPolicy(IMMEDIATE) .get(); query = nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg) .innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false); response = client().prepareSearch("index2") .setQuery(query) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); assertHitCount(response, 1); }
public void testNestedSourceFiltering() throws Exception { assertAcked(prepareCreate("index1").addMapping("message", "comments", "type=nested")); client().prepareIndex("index1", "message", "1").setSource(jsonBuilder().startObject() .field("message", "quick brown fox") .startArray("comments") .startObject().field("message", "fox eat quick").endObject() .startObject().field("message", "fox ate rabbit x y z").endObject() .startObject().field("message", "rabbit got away").endObject() .endArray() .endObject()).get(); refresh(); // the field name (comments.message) used for source filtering should be the same as when using that field for // other features (like in the query dsl or aggs) in order for consistency: SearchResponse response = client().prepareSearch() .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.None) .innerHit(new InnerHitBuilder().setFetchSourceContext(new FetchSourceContext(true, new String[]{"comments.message"}, null)), false)) .get(); assertNoFailures(response); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getTotalHits(), equalTo(2L)); assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getSourceAsMap()), equalTo("fox eat quick")); assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(1).getSourceAsMap()), equalTo("fox ate rabbit x y z")); }
public void testInnerHitsWithIgnoreUnmapped() throws Exception { assertAcked(prepareCreate("index1") .addMapping("parent_type", "nested_type", "type=nested") .addMapping("child_type", "_parent", "type=parent_type") ); createIndex("index2"); client().prepareIndex("index1", "parent_type", "1").setSource("nested_type", Collections.singletonMap("key", "value")).get(); client().prepareIndex("index1", "child_type", "2").setParent("1").setSource("{}", XContentType.JSON).get(); client().prepareIndex("index2", "type", "3").setSource("key", "value").get(); refresh(); SearchResponse response = client().prepareSearch("index1", "index2") .setQuery(boolQuery() .should(nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) .innerHit(new InnerHitBuilder(), true)) .should(termQuery("key", "value")) ) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); response = client().prepareSearch("index1", "index2") .setQuery(boolQuery() .should(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) .innerHit(new InnerHitBuilder(), true)) .should(termQuery("key", "value")) ) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); }
public void testExplain() throws Exception { assertAcked(prepareCreate("test") .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") .startObject("nested1") .field("type", "nested") .endObject() .endObject().endObject().endObject())); ensureGreen(); client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() .field("field1", "value1") .startArray("nested1") .startObject() .field("n_field1", "n_value1") .endObject() .startObject() .field("n_field1", "n_value1") .endObject() .endArray() .endObject()) .setRefreshPolicy(IMMEDIATE) .execute().actionGet(); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1"), ScoreMode.Total)) .setExplain(true) .execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); Explanation explanation = searchResponse.getHits().getHits()[0].getExplanation(); assertThat(explanation.getValue(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertThat(explanation.toString(), startsWith("0.36464313 = Score based on 2 child docs in range from 0 to 1")); }
public void testCachingBugWithFqueryFilter() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); List<IndexRequestBuilder> builders = new ArrayList<>(); // index simple data for (int i = 0; i < 10; i++) { builders.add(client().prepareIndex("test", "parent", Integer.toString(i)).setSource("p_field", i)); } indexRandom(randomBoolean(), builders); builders.clear(); for (int j = 0; j < 2; j++) { for (int i = 0; i < 10; i++) { builders.add(client().prepareIndex("test", "child", Integer.toString(i)).setSource("c_field", i).setParent("" + 0)); } for (int i = 0; i < 10; i++) { builders.add(client().prepareIndex("test", "child", Integer.toString(i + 10)).setSource("c_field", i + 10).setParent(Integer.toString(i))); } if (randomBoolean()) { break; // randomly break out and dont' have deletes / updates } } indexRandom(true, builders); for (int i = 1; i <= 10; i++) { logger.info("Round {}", i); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Max))) .get(); assertNoFailures(searchResponse); searchResponse = client().prepareSearch("test") .setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery(), true))) .get(); assertNoFailures(searchResponse); } }
public void testDeletedParent() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); // index simple data client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1").get(); client().prepareIndex("test", "child", "c1").setSource("c_field", "red").setParent("p1").get(); client().prepareIndex("test", "child", "c2").setSource("c_field", "yellow").setParent("p1").get(); client().prepareIndex("test", "parent", "p2").setSource("p_field", "p_value2").get(); client().prepareIndex("test", "child", "c3").setSource("c_field", "blue").setParent("p2").get(); client().prepareIndex("test", "child", "c4").setSource("c_field", "red").setParent("p2").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1\"")); // update p1 and see what that we get updated values... client().prepareIndex("test", "parent", "p1").setSource("p_field", "p_value1_updated").get(); client().admin().indices().prepareRefresh().get(); searchResponse = client().prepareSearch("test") .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1_updated\"")); }
public void testCountApiUsage() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); String parentId = "p1"; client().prepareIndex("test", "parent", parentId).setSource("p_field", "1").get(); client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get(); refresh(); SearchResponse countResponse = client().prepareSearch("test").setSize(0) .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertHitCount(countResponse, 1L); countResponse = client().prepareSearch("test").setSize(0).setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) .get(); assertHitCount(countResponse, 1L); countResponse = client().prepareSearch("test").setSize(0) .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))) .get(); assertHitCount(countResponse, 1L); countResponse = client().prepareSearch("test").setSize(0).setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1"), false))) .get(); assertHitCount(countResponse, 1L); }
public void testExplainUsage() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); String parentId = "p1"; client().prepareIndex("test", "parent", parentId).setSource("p_field", "1").get(); client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test") .setExplain(true) .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1")); searchResponse = client().prepareSearch("test") .setExplain(true) .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1")); ExplainResponse explainResponse = client().prepareExplain("test", "parent", parentId) .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertThat(explainResponse.isExists(), equalTo(true)); assertThat(explainResponse.getExplanation().getDetails()[0].getDescription(), containsString("join value p1")); }
public void testParentChildQueriesCanHandleNoRelevantTypesInIndex() throws Exception { assertAcked(prepareCreate("test") .addMapping("parent") .addMapping("child", "_parent", "type=parent")); ensureGreen(); SearchResponse response = client().prepareSearch("test") .setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits(), equalTo(0L)); client().prepareIndex("test", "child1").setSource(jsonBuilder().startObject().field("text", "value").endObject()) .setRefreshPolicy(RefreshPolicy.IMMEDIATE).get(); response = client().prepareSearch("test") .setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits(), equalTo(0L)); response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.Max)) .get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits(), equalTo(0L)); response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"), false)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits(), equalTo(0L)); response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"), true)) .get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits(), equalTo(0L)); }