/** * Checks the result of {@link QueryBuilder#toQuery(QueryShardContext)} given the original {@link QueryBuilder} * and {@link QueryShardContext}. Verifies that named queries and boost are properly handled and delegates to * {@link #doAssertLuceneQuery(AbstractQueryBuilder, Query, SearchContext)} for query specific checks. */ private void assertLuceneQuery(QB queryBuilder, Query query, SearchContext context) throws IOException { if (queryBuilder.queryName() != null) { Query namedQuery = context.getQueryShardContext().copyNamedQueries().get(queryBuilder.queryName()); assertThat(namedQuery, equalTo(query)); } if (query != null) { if (queryBuilder.boost() != AbstractQueryBuilder.DEFAULT_BOOST) { assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(SpanBoostQuery.class))); if (query instanceof SpanBoostQuery) { SpanBoostQuery spanBoostQuery = (SpanBoostQuery) query; assertThat(spanBoostQuery.getBoost(), equalTo(queryBuilder.boost())); query = spanBoostQuery.getQuery(); } else { BoostQuery boostQuery = (BoostQuery) query; assertThat(boostQuery.getBoost(), equalTo(queryBuilder.boost())); query = boostQuery.getQuery(); } } } doAssertLuceneQuery(queryBuilder, query, context); }
private <P extends AbstractQueryBuilder<P>> void phraseBoostTestCaseForClauses(String highlighterType, float boost, QueryBuilder terms, P phrase) { Matcher<String> highlightedMatcher = Matchers.either(containsString("<em>highlight words together</em>")).or( containsString("<em>highlight</em> <em>words</em> <em>together</em>")); SearchRequestBuilder search = client().prepareSearch("test").highlighter( new HighlightBuilder().field("field1", 100, 1).order("score").highlighterType(highlighterType).requireFieldMatch(true)); // Try with a bool query phrase.boost(boost); SearchResponse response = search.setQuery(boolQuery().must(terms).should(phrase)).get(); assertHighlight(response, 0, "field1", 0, 1, highlightedMatcher); phrase.boost(1); // Try with a boosting query response = search.setQuery(boostingQuery(phrase, terms).boost(boost).negativeBoost(1)).get(); assertHighlight(response, 0, "field1", 0, 1, highlightedMatcher); // Try with a boosting query using a negative boost response = search.setQuery(boostingQuery(phrase, terms).boost(1).negativeBoost(1/boost)).get(); assertHighlight(response, 0, "field1", 0, 1, highlightedMatcher); }
protected static void assertTermOrBoostQuery(Query query, String field, String value, float fieldBoost) { if (fieldBoost != AbstractQueryBuilder.DEFAULT_BOOST) { assertThat(query, instanceOf(BoostQuery.class)); BoostQuery boostQuery = (BoostQuery) query; assertThat(boostQuery.getBoost(), equalTo(fieldBoost)); query = boostQuery.getQuery(); } assertTermQuery(query, field, value); }
protected QueryBuilder parseQuery(AbstractQueryBuilder<?> builder) throws IOException { return parseQuery(createParser(JsonXContent.jsonXContent, builder.buildAsBytes(XContentType.JSON))); }
public void testParseSingleFunction() throws IOException { String functionScoreQuery = "{\n" + " \"function_score\":{\n" + " \"query\":{\n" + " \"term\":{\n" + " \"field1\":\"value1\"\n" + " }\n" + " },\n" + " \"gauss\": {\n" + " \"field_name\": {\n" + " \"origin\":0.5,\n" + " \"scale\":0.6\n" + " }\n" + " },\n" + " \"boost\" : 3,\n" + " \"score_mode\" : \"avg\",\n" + " \"boost_mode\" : \"replace\",\n" + " \"max_boost\" : 10\n" + " }\n" + "}"; QueryBuilder queryBuilder = parseQuery(functionScoreQuery); /* * given that we copy part of the decay functions as bytes, we test that fromXContent and toXContent both work no matter what the * initial format was */ for (int i = 0; i <= XContentType.values().length; i++) { assertThat(queryBuilder, instanceOf(FunctionScoreQueryBuilder.class)); FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) queryBuilder; assertThat(functionScoreQueryBuilder.query(), instanceOf(TermQueryBuilder.class)); TermQueryBuilder termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.query(); assertThat(termQueryBuilder.fieldName(), equalTo("field1")); assertThat(termQueryBuilder.value(), equalTo("value1")); assertThat(functionScoreQueryBuilder.filterFunctionBuilders().length, equalTo(1)); assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(MatchAllQueryBuilder.class)); assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class)); GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder .filterFunctionBuilders()[0].getScoreFunction(); assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name")); assertThat(gaussDecayFunctionBuilder.getWeight(), nullValue()); assertThat(functionScoreQueryBuilder.boost(), equalTo(3f)); assertThat(functionScoreQueryBuilder.scoreMode(), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG)); assertThat(functionScoreQueryBuilder.boostMode(), equalTo(CombineFunction.REPLACE)); assertThat(functionScoreQueryBuilder.maxBoost(), equalTo(10f)); if (i < XContentType.values().length) { BytesReference bytes = ((AbstractQueryBuilder) queryBuilder).buildAsBytes(XContentType.values()[i]); try (XContentParser parser = createParser(XContentType.values()[i].xContent(), bytes)) { queryBuilder = parseQuery(parser); } } } }
public static Optional<PhraseCountQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); String fieldName = null; Object value = null; float boost = AbstractQueryBuilder.DEFAULT_BOOST; String analyzer = null; int slop = MatchQuery.DEFAULT_PHRASE_SLOP; boolean inOrder = false; boolean weightedCount = false; String queryName = null; String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (PhraseCountQueryBuilder.QUERY_FIELD.match(currentFieldName)) { value = parser.objectText(); } else if (ANALYZER_FIELD.match(currentFieldName)) { analyzer = parser.text(); } else if(IN_ORDER_FIELD.match(currentFieldName)) { inOrder = parser.booleanValue(); } else if (WEIGHTED_COUNT_FIELD.match(currentFieldName)) { weightedCount = parser.booleanValue(); } else if (BOOST_FIELD.match(currentFieldName)) { boost = parser.floatValue(); } else if (SLOP_FIELD.match(currentFieldName)) { slop = parser.intValue(); } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]"); } } else { throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"); } } } else { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = parser.currentName(); value = parser.objectText(); } } PhraseCountQueryBuilder phraseCountQuery = new PhraseCountQueryBuilder(fieldName, value); phraseCountQuery.analyzer(analyzer); phraseCountQuery.slop(slop); phraseCountQuery.inOrder(inOrder); phraseCountQuery.weightedCount(weightedCount); phraseCountQuery.queryName(queryName); phraseCountQuery.boost(boost); return Optional.of(phraseCountQuery); }