private void assertQueryBuilder(BytesRef actual, QueryBuilder expected) throws IOException {
   XContentParser sourceParser =
       PercolatorFieldMapper.QUERY_BUILDER_CONTENT_TYPE
           .xContent()
           .createParser(actual.bytes, actual.offset, actual.length);
   QueryParseContext qsc = indexService.newQueryShardContext().newParseContext(sourceParser);
   assertThat(qsc.parseInnerQueryBuilder().get(), equalTo(expected));
 }
  @Override
  protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.getShard(request.shardId().id());
    final QueryShardContext queryShardContext = indexService.newQueryShardContext();
    queryShardContext.setTypes(request.types());

    boolean valid;
    String explanation = null;
    String error = null;
    Engine.Searcher searcher = indexShard.acquireSearcher("validate_query");

    DefaultSearchContext searchContext =
        new DefaultSearchContext(
            0,
            new ShardSearchLocalRequest(
                request.types(), request.nowInMillis(), request.filteringAliases()),
            null,
            searcher,
            indexService,
            indexShard,
            scriptService,
            pageCacheRecycler,
            bigArrays,
            threadPool.estimatedTimeInMillisCounter(),
            parseFieldMatcher,
            SearchService.NO_TIMEOUT);
    SearchContext.setCurrent(searchContext);
    try {
      searchContext.parsedQuery(queryShardContext.toQuery(request.query()));
      searchContext.preProcess();

      valid = true;
      if (request.rewrite()) {
        explanation = getRewrittenQuery(searcher.searcher(), searchContext.query());
      } else if (request.explain()) {
        explanation = searchContext.filteredQuery().query().toString();
      }
    } catch (QueryShardException | ParsingException e) {
      valid = false;
      error = e.getDetailedMessage();
    } catch (AssertionError | IOException e) {
      valid = false;
      error = e.getMessage();
    } finally {
      searchContext.close();
      SearchContext.removeCurrent();
    }

    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
  }
 public void testQueryWithRewrite() throws Exception {
   addQueryMapping();
   client().prepareIndex("remote", "type", "1").setSource("field", "value").get();
   QueryBuilder queryBuilder =
       termsLookupQuery("field", new TermsLookup("remote", "type", "1", "field"));
   ParsedDocument doc =
       mapperService
           .documentMapper(typeName)
           .parse(
               "test",
               typeName,
               "1",
               XContentFactory.jsonBuilder()
                   .startObject()
                   .field(fieldName, queryBuilder)
                   .endObject()
                   .bytes());
   BytesRef qbSource =
       doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName())[0].binaryValue();
   assertQueryBuilder(qbSource, queryBuilder.rewrite(indexService.newQueryShardContext()));
 }