use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.
the class FunctionScoreQueryBuilder method parseFiltersAndFunctions.
private static String parseFiltersAndFunctions(QueryParseContext parseContext, List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders) throws IOException {
String currentFieldName = null;
XContentParser.Token token;
XContentParser parser = parseContext.parser();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
QueryBuilder filter = null;
ScoreFunctionBuilder<?> scoreFunction = null;
Float functionWeight = null;
if (token != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}]. malformed query, expected a [{}] while parsing functions but got a [{}] instead", XContentParser.Token.START_OBJECT, token, NAME);
} else {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (FILTER_FIELD.match(currentFieldName)) {
filter = parseContext.parseInnerQueryBuilder();
} else {
if (scoreFunction != null) {
throw new ParsingException(parser.getTokenLocation(), "failed to parse function_score functions. already found [{}], now encountering [{}].", scoreFunction.getName(), currentFieldName);
}
scoreFunction = parser.namedObject(ScoreFunctionBuilder.class, currentFieldName, parseContext);
}
} else if (token.isValue()) {
if (WEIGHT_FIELD.match(currentFieldName)) {
functionWeight = parser.floatValue();
} else {
throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. field [{}] is not supported", NAME, currentFieldName);
}
}
}
if (functionWeight != null) {
if (scoreFunction == null) {
scoreFunction = new WeightBuilder().setWeight(functionWeight);
} else {
scoreFunction.setWeight(functionWeight);
}
}
}
if (filter == null) {
filter = new MatchAllQueryBuilder();
}
if (scoreFunction == null) {
throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. an entry in functions list is missing a function.", NAME);
}
filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(filter, scoreFunction));
}
return currentFieldName;
}
use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.
the class TemplateQueryBuilderTests method testMustRewrite.
@Override
public void testMustRewrite() throws IOException {
String query = "{ \"match_all\" : {}}";
QueryBuilder builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query, Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap()));
try {
builder.toQuery(createShardContext());
fail();
} catch (UnsupportedOperationException ex) {
assertEquals("this query must be rewritten first", ex.getMessage());
}
assertEquals(new MatchAllQueryBuilder(), builder.rewrite(createShardContext()));
}
use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.
the class NestedHelperTests method testNested.
public void testNested() throws IOException {
QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// this time we do not add a filter since the inner query only matches inner docs
expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_parent
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
// we need to add the filter again because of include_in_root
expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
assertEquals(expectedChildQuery, query.getChildQuery());
assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.
the class FunctionScoreIT method testMinScoreFunctionScoreBasic.
public void testMinScoreFunctionScoreBasic() throws IOException {
index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).endObject());
refresh();
float score = randomFloat();
float minScore = randomFloat();
index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).field("random_score", // Pass the random score as a document field so that it can be extracted in the script
score).endObject());
refresh();
ensureYellow();
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['random_score']", Collections.emptyMap());
SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)))).actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits(), is(0L));
} else {
assertThat(searchResponse.getHits().getTotalHits(), is(1L));
}
searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)))).actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits(), is(0L));
} else {
assertThat(searchResponse.getHits().getTotalHits(), is(1L));
}
}
use of org.elasticsearch.index.query.MatchAllQueryBuilder in project elasticsearch by elastic.
the class HighlightBuilderTests method setRandomCommonOptions.
@SuppressWarnings({ "rawtypes" })
private static void setRandomCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
if (randomBoolean()) {
// need to set this together, otherwise parsing will complain
highlightBuilder.preTags(randomStringArray(0, 3));
highlightBuilder.postTags(randomStringArray(0, 3));
}
if (randomBoolean()) {
highlightBuilder.fragmentSize(randomIntBetween(0, 100));
}
if (randomBoolean()) {
highlightBuilder.numOfFragments(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.highlighterType(randomAsciiOfLengthBetween(1, 10));
}
if (randomBoolean()) {
highlightBuilder.fragmenter(randomAsciiOfLengthBetween(1, 10));
}
if (randomBoolean()) {
QueryBuilder highlightQuery;
switch(randomInt(2)) {
case 0:
highlightQuery = new MatchAllQueryBuilder();
break;
case 1:
highlightQuery = new IdsQueryBuilder();
break;
default:
case 2:
highlightQuery = new TermQueryBuilder(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
break;
}
highlightQuery.boost((float) randomDoubleBetween(0, 10, false));
highlightBuilder.highlightQuery(highlightQuery);
}
if (randomBoolean()) {
if (randomBoolean()) {
highlightBuilder.order(randomFrom(Order.values()));
} else {
// also test the string setter
highlightBuilder.order(randomFrom(Order.values()).toString());
}
}
if (randomBoolean()) {
highlightBuilder.highlightFilter(randomBoolean());
}
if (randomBoolean()) {
highlightBuilder.forceSource(randomBoolean());
}
if (randomBoolean()) {
if (randomBoolean()) {
highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()));
} else {
// also test the string setter
highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()).toString());
}
}
if (randomBoolean()) {
highlightBuilder.boundaryMaxScan(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.boundaryChars(randomAsciiOfLengthBetween(1, 10).toCharArray());
}
if (randomBoolean()) {
highlightBuilder.boundaryScannerLocale(randomLocale(random()).toLanguageTag());
}
if (randomBoolean()) {
highlightBuilder.noMatchSize(randomIntBetween(0, 10));
}
if (randomBoolean()) {
highlightBuilder.phraseLimit(randomIntBetween(0, 10));
}
if (randomBoolean()) {
int items = randomIntBetween(0, 5);
Map<String, Object> options = new HashMap<>(items);
for (int i = 0; i < items; i++) {
Object value = null;
switch(randomInt(2)) {
case 0:
value = randomAsciiOfLengthBetween(1, 10);
break;
case 1:
value = new Integer(randomInt(1000));
break;
case 2:
value = new Boolean(randomBoolean());
break;
}
options.put(randomAsciiOfLengthBetween(1, 10), value);
}
}
if (randomBoolean()) {
highlightBuilder.requireFieldMatch(randomBoolean());
}
}
Aggregations