Package crate.elasticsearch.action.searchinto.parser

Source Code of crate.elasticsearch.action.searchinto.parser.SearchIntoParser

package crate.elasticsearch.action.searchinto.parser;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.fetch.FetchPhase;
import org.elasticsearch.search.fetch.explain.ExplainParseElement;
import org.elasticsearch.search.query.QueryPhase;

import crate.elasticsearch.action.searchinto.SearchIntoContext;

/**
* Parser for payload given to _search_into action.
*/
public class SearchIntoParser extends AbstractSearchIntoParser implements ISearchIntoParser {

    private final ImmutableMap<String, SearchParseElement> elementParsers;

    @Inject
    public SearchIntoParser(QueryPhase queryPhase, FetchPhase fetchPhase) {
        Map<String, SearchParseElement> elementParsers = new HashMap<String,
                SearchParseElement>();
        elementParsers.putAll(queryPhase.parseElements());
        elementParsers.put("fields", new FieldsParseElement());
        elementParsers.put("targetNodes", new TargetNodesParseElement());
        elementParsers.put("explain", new ExplainParseElement());
        this.elementParsers = ImmutableMap.copyOf(elementParsers);
    }

    @Override
    protected void validate(SearchIntoContext context) {
        if (!context.hasFieldNames()) {
            throw new SearchParseException(context, "No fields defined");
        }

        for (String field : context.fieldNames()) {
            FieldMapper<?> mapper = context.mapperService().smartNameFieldMapper(
                    field);
            if (mapper == null && !field.equals(
                    "_version") && !field.startsWith(
                    FieldsParseElement.SCRIPT_FIELD_PREFIX)) {
                throw new SearchParseException(context,
                        "SearchInto field [" + field + "] does not exist in " +
                                "the mapping");
            }
        }
        super.validate(context);
    }

    @Override
    protected ImmutableMap<String, SearchParseElement> getElementParsers() {
        return elementParsers;
    }


}
TOP

Related Classes of crate.elasticsearch.action.searchinto.parser.SearchIntoParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.