use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class HighlightBuilderTests method testFromXContent.
/**
* creates random highlighter, renders it to xContent and back to new instance that should be equal to original
*/
public void testFromXContent() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
HighlightBuilder highlightBuilder = randomHighlighterBuilder();
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
if (randomBoolean()) {
builder.prettyPrint();
}
highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser parser = createParser(shuffled);
QueryParseContext context = new QueryParseContext(parser);
parser.nextToken();
HighlightBuilder secondHighlightBuilder;
try {
secondHighlightBuilder = HighlightBuilder.fromXContent(context);
} catch (RuntimeException e) {
throw new RuntimeException("Error parsing " + highlightBuilder, e);
}
assertNotSame(highlightBuilder, secondHighlightBuilder);
assertEquals(highlightBuilder, secondHighlightBuilder);
assertEquals(highlightBuilder.hashCode(), secondHighlightBuilder.hashCode());
}
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class SearchSourceBuilderTests method assertParseSearchSource.
private void assertParseSearchSource(SearchSourceBuilder testBuilder, XContentParser parser) throws IOException {
QueryParseContext parseContext = new QueryParseContext(parser);
if (randomBoolean()) {
// sometimes we move it on the START_OBJECT to
parser.nextToken();
// test the embedded case
}
SearchSourceBuilder newBuilder = SearchSourceBuilder.fromXContent(parseContext);
assertNull(parser.nextToken());
assertEquals(testBuilder, newBuilder);
assertEquals(testBuilder.hashCode(), newBuilder.hashCode());
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class IncludeExcludeTests method serializeMixedRegex.
// Serializes/deserializes the IncludeExclude statement with include AND
// exclude clauses
private IncludeExclude serializeMixedRegex(IncludeExclude incExc) throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
if (randomBoolean()) {
builder.prettyPrint();
}
builder.startObject();
incExc.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser parser = createParser(builder);
QueryParseContext parseContext = new QueryParseContext(parser);
XContentParser.Token token = parser.nextToken();
assertEquals(token, XContentParser.Token.START_OBJECT);
IncludeExclude inc = null;
IncludeExclude exc = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
assertEquals(XContentParser.Token.FIELD_NAME, token);
if (IncludeExclude.INCLUDE_FIELD.match(parser.currentName())) {
token = parser.nextToken();
inc = IncludeExclude.parseInclude(parser, parseContext);
} else if (IncludeExclude.EXCLUDE_FIELD.match(parser.currentName())) {
token = parser.nextToken();
exc = IncludeExclude.parseExclude(parser, parseContext);
} else {
throw new IllegalArgumentException("Unexpected field name serialized in test: " + parser.currentName());
}
}
assertNotNull(inc);
assertNotNull(exc);
// Include and Exclude clauses are parsed independently and then merged
return IncludeExclude.merge(inc, exc);
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class TemplateQueryBuilder method doRewrite.
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
BytesReference querySource = queryRewriteContext.getTemplateBytes(template);
try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(queryRewriteContext.getXContentRegistry(), querySource)) {
final QueryParseContext queryParseContext = queryRewriteContext.newParseContext(qSourceParser);
final QueryBuilder queryBuilder = queryParseContext.parseInnerQueryBuilder();
if (boost() != DEFAULT_BOOST || queryName() != null) {
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(queryBuilder);
return boolQueryBuilder;
}
return queryBuilder;
}
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class TransportSearchTemplateAction method doExecute.
@Override
protected void doExecute(SearchTemplateRequest request, ActionListener<SearchTemplateResponse> listener) {
final SearchTemplateResponse response = new SearchTemplateResponse();
try {
Script script = new Script(request.getScriptType(), TEMPLATE_LANG, request.getScript(), request.getScriptParams() == null ? Collections.emptyMap() : request.getScriptParams());
ExecutableScript executable = scriptService.executable(script, SEARCH);
BytesReference source = (BytesReference) executable.run();
response.setSource(source);
if (request.isSimulate()) {
listener.onResponse(response);
return;
}
// Executes the search
SearchRequest searchRequest = request.getRequest();
//we can assume the template is always json as we convert it before compiling it
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, source)) {
SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
builder.parseXContent(new QueryParseContext(parser));
builder.explain(request.isExplain());
builder.profile(request.isProfile());
searchRequest.source(builder);
searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
try {
response.setResponse(searchResponse);
listener.onResponse(response);
} catch (Exception t) {
listener.onFailure(t);
}
}
@Override
public void onFailure(Exception t) {
listener.onFailure(t);
}
});
}
} catch (Exception t) {
listener.onFailure(t);
}
}
Aggregations