use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FunctionScoreQueryBuilderTests method testParseFunctionsArray.
public void testParseFunctionsArray() throws IOException {
String functionScoreQuery = "{\n" + " \"function_score\":{\n" + " \"query\":{\n" + " \"term\":{\n" + " \"field1\":\"value1\"\n" + " }\n" + " },\n" + " \"functions\": [\n" + " {\n" + " \"random_score\": {\n" + " \"seed\":123456\n" + " },\n" + " \"weight\": 3,\n" + " \"filter\": {\n" + " \"term\":{\n" + " \"field2\":\"value2\"\n" + " }\n" + " }\n" + " },\n" + " {\n" + " \"filter\": {\n" + " \"term\":{\n" + " \"field3\":\"value3\"\n" + " }\n" + " },\n" + " \"weight\": 9\n" + " },\n" + " {\n" + " \"gauss\": {\n" + " \"field_name\": {\n" + " \"origin\":0.5,\n" + " \"scale\":0.6\n" + " }\n" + " }\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(3));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(TermQueryBuilder.class));
termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter();
assertThat(termQueryBuilder.fieldName(), equalTo("field2"));
assertThat(termQueryBuilder.value(), equalTo("value2"));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter(), instanceOf(TermQueryBuilder.class));
termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter();
assertThat(termQueryBuilder.fieldName(), equalTo("field3"));
assertThat(termQueryBuilder.value(), equalTo("value3"));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getFilter(), instanceOf(MatchAllQueryBuilder.class));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(RandomScoreFunctionBuilder.class));
RandomScoreFunctionBuilder randomScoreFunctionBuilder = (RandomScoreFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
assertThat(randomScoreFunctionBuilder.getSeed(), equalTo(123456));
assertThat(randomScoreFunctionBuilder.getWeight(), equalTo(3f));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction(), instanceOf(WeightBuilder.class));
WeightBuilder weightBuilder = (WeightBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction();
assertThat(weightBuilder.getWeight(), equalTo(9f));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction();
assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
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);
}
}
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FunctionScoreQueryBuilderTests method testRewriteWithFunction.
public void testRewriteWithFunction() throws IOException {
QueryBuilder firstFunction = new WrapperQueryBuilder(new TermQueryBuilder("tq", "1").toString());
TermQueryBuilder secondFunction = new TermQueryBuilder("tq", "2");
QueryBuilder queryBuilder = randomBoolean() ? new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()) : new TermQueryBuilder("foo", "bar");
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(firstFunction, new RandomScoreFunctionBuilder()), new FunctionScoreQueryBuilder.FilterFunctionBuilder(secondFunction, new RandomScoreFunctionBuilder()) });
FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
assertNotSame(functionScoreQueryBuilder, rewrite);
assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
assertEquals(rewrite.filterFunctionBuilders()[0].getFilter(), new TermQueryBuilder("tq", "1"));
assertSame(rewrite.filterFunctionBuilders()[1].getFilter(), secondFunction);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FunctionScoreQueryBuilderTests method testWeight1fStillProducesWeightFunction.
public void testWeight1fStillProducesWeightFunction() throws IOException {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
String queryString = jsonBuilder().startObject().startObject("function_score").startArray("functions").startObject().startObject("field_value_factor").field("field", INT_FIELD_NAME).endObject().field("weight", 1.0).endObject().endArray().endObject().endObject().string();
QueryBuilder query = parseQuery(queryString);
assertThat(query, instanceOf(FunctionScoreQueryBuilder.class));
FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) query;
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(FieldValueFactorFunctionBuilder.class));
FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = (FieldValueFactorFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
assertThat(fieldValueFactorFunctionBuilder.fieldName(), equalTo(INT_FIELD_NAME));
assertThat(fieldValueFactorFunctionBuilder.factor(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_FACTOR));
assertThat(fieldValueFactorFunctionBuilder.modifier(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_MODIFIER));
assertThat(fieldValueFactorFunctionBuilder.getWeight(), equalTo(1f));
assertThat(fieldValueFactorFunctionBuilder.missing(), nullValue());
Query luceneQuery = query.toQuery(createShardContext());
assertThat(luceneQuery, instanceOf(FunctionScoreQuery.class));
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) luceneQuery;
assertThat(functionScoreQuery.getFunction(), instanceOf(WeightFactorFunction.class));
WeightFactorFunction weightFactorFunction = (WeightFactorFunction) functionScoreQuery.getFunction();
assertThat(weightFactorFunction.getWeight(), equalTo(1.0f));
assertThat(weightFactorFunction.getScoreFunction(), instanceOf(FieldValueFactorFunction.class));
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class AbstractQueryTestCase method rewriteQuery.
private QueryBuilder rewriteQuery(QB queryBuilder, QueryRewriteContext rewriteContext) throws IOException {
QueryBuilder rewritten = QueryBuilder.rewriteQuery(queryBuilder, rewriteContext);
// extra safety to fail fast - serialize the rewritten version to ensure it's serializable.
assertSerialization(rewritten);
return rewritten;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class AbstractQueryTestCase method parseQuery.
protected static QueryBuilder parseQuery(XContentParser parser) throws IOException {
QueryParseContext context = createParseContext(parser);
QueryBuilder parseInnerQueryBuilder = context.parseInnerQueryBuilder();
assertNull(parser.nextToken());
return parseInnerQueryBuilder;
}
Aggregations