/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.index.query.plugin;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Weight;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.query.QueryParser;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.SearchModule;

import java.io.IOException;

public class DummyQueryParserPlugin extends Plugin {

    @Override
    public String name() {
        return "dummy";
    }

    @Override
    public String description() {
        return "dummy query";
    }

    public void onModule(SearchModule module) {
        module.registerQueryParser(new DummyQueryParser(), new ParseField(DummyQueryBuilder.NAME));
    }

    public static class DummyQueryBuilder extends AbstractQueryBuilder<DummyQueryBuilder> {
        private static final String NAME = "dummy";

        @Override
        protected void doXContent(XContentBuilder builder, Params params) throws IOException {
            builder.startObject(NAME).endObject();
        }

        @Override
        protected Query doToQuery(QueryShardContext context) throws IOException {
            return new DummyQuery(context.isFilter());
        }

        @Override
        protected DummyQueryBuilder doReadFrom(StreamInput in) throws IOException {
            return new DummyQueryBuilder();
        }

        @Override
        protected void doWriteTo(StreamOutput out) throws IOException {
            // Do Nothing
        }

        @Override
        protected int doHashCode() {
            return 0;
        }

        @Override
        protected boolean doEquals(DummyQueryBuilder other) {
            return true;
        }

        @Override
        public String getWriteableName() {
            return NAME;
        }
    }

    public static class DummyQueryParser implements QueryParser<DummyQueryBuilder> {
        @Override
        public DummyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
            XContentParser.Token token = parseContext.parser().nextToken();
            assert token == XContentParser.Token.END_OBJECT;
            return new DummyQueryBuilder();
        }

        @Override
        public DummyQueryBuilder getBuilderPrototype() {
            return new DummyQueryBuilder();
        }
    }

    public static class DummyQuery extends Query {
        public final boolean isFilter;
        private final Query matchAllDocsQuery = new MatchAllDocsQuery();

        private DummyQuery(boolean isFilter) {
            this.isFilter = isFilter;
        }

        @Override
        public String toString(String field) {
            return getClass().getSimpleName();
        }

        @Override
        public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
            return matchAllDocsQuery.createWeight(searcher, needsScores);
        }
    }
}
@@ -1396,58 +1396,8 @@
 xt;%0A
-import org.elasticsearch.index.query.QueryParser;%0A
 impo
@@ -1440,32 +1440,32 @@
 ryShardContext;%0A
+
 import org.elast
@@ -1852,22 +1852,16 @@
 terQuery
-Parser
 (new Dum
@@ -1871,33 +1871,62 @@
 uery
-Parser(), new ParseField(
+Builder()::readFrom, DummyQueryBuilder::fromXContent, 
 Dumm
@@ -1939,21 +1939,32 @@
 Builder.
-NAME)
+QUERY_NAME_FIELD
 );%0A    %7D
@@ -2108,16 +2108,97 @@
 %22dummy%22;
+%0A        private static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);
 %0A%0A      
@@ -2358,32 +2358,330 @@
 t();%0A        %7D%0A%0A
+        public static DummyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException %7B%0A            XContentParser.Token token = parseContext.parser().nextToken();%0A            assert token == XContentParser.Token.END_OBJECT;%0A            return new DummyQueryBuilder();%0A        %7D%0A%0A
         @Overrid
@@ -3371,32 +3371,32 @@
 iteableName() %7B%0A
+
             retu
@@ -3425,538 +3425,8 @@
  %7D%0A%0A
-    public static class DummyQueryParser implements QueryParser%3CDummyQueryBuilder%3E %7B%0A        @Override%0A        public DummyQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException %7B%0A            XContentParser.Token token = parseContext.parser().nextToken();%0A            assert token == XContentParser.Token.END_OBJECT;%0A            return new DummyQueryBuilder();%0A        %7D%0A%0A        @Override%0A        public DummyQueryBuilder getBuilderPrototype() %7B%0A            return new DummyQueryBuilder();%0A        %7D%0A    %7D%0A%0A
     

Cut CustomQueryParserIT to registerQuery