org.elasticsearch.action.debugquery.QueryToMapTransformer.java Source code

Java tutorial

Introduction

Here is the source code for org.elasticsearch.action.debugquery.QueryToMapTransformer.java

Source

/*
 * 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.action.debugquery;

import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.elasticsearch.query.visitor.QueryVisitor;
import org.elasticsearch.query.visitor.QueryAcceptor;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class QueryToMapTransformer implements QueryVisitor<Map> {
    private final IndexSearcher searcher;

    public QueryToMapTransformer(IndexSearcher searcher) {
        this.searcher = searcher;
    }

    private Map<String, Object> newQuery(final Query query) {
        final Map<String, Object> map = new LinkedHashMap<>();
        map.put("class", query.getClass().getName());
        map.put("boost", query.getBoost());
        return map;
    }

    private String getTermField(final Term term) {
        final String field = term.field();
        if (field == null) {
            return "_NULL_";
        } else if (field.isEmpty()) {
            return "_EMPTY_";
        } else {
            return field;
        }
    }

    @Override
    public Map visitUnknownQuery(Query query) throws Exception {
        final Map<String, Object> map = newQuery(query);
        map.put("toString", query.toString());
        return map;
    }

    @Override
    public Map visit(BooleanQuery query) throws Exception {
        final Map map = newQuery(query);
        map.put("minShouldMatch", query.getMinimumNumberShouldMatch());
        map.put("coordDisabled", query.isCoordDisabled());
        final ArrayList clauses = new ArrayList(query.clauses().size());
        map.put("clauses", clauses);
        for (final BooleanClause clause : query.clauses()) {
            final Object child = QueryAcceptor.accept(clause.getQuery(), this);
            final Map<String, Object> clauseMap = new LinkedHashMap();
            clauses.add(clauseMap);
            clauseMap.put("occur", clause.getOccur().name());
            clauseMap.put("query", child);
        }
        return map;
    }

    @Override
    public Map visit(DisjunctionMaxQuery query) throws Exception {
        final Map map = newQuery(query);
        map.put("tieBreaker", query.getTieBreakerMultiplier());
        final ArrayList children = new ArrayList(query.getDisjuncts().size());
        map.put("disjuncts", children);
        for (final Query childQuery : query.getDisjuncts()) {
            final Object childObj = QueryAcceptor.accept(childQuery, this);
            children.add(childObj);
        }
        return map;
    }

    @Override
    public Map visit(TermQuery query) throws Exception {
        final Map map = newQuery(query);
        final Term term = query.getTerm();
        final String field = getTermField(term);
        final String termStr = term.bytes().utf8ToString();
        map.put(field, termStr);
        TermContext termCtx = TermContext.build(searcher.getTopReaderContext(), term);
        map.put("doc_freq", termCtx.docFreq());
        return map;
    }

    @Override
    public Map visit(PhraseQuery query) throws Exception {
        final Map map = newQuery(query);
        map.put("slop", query.getSlop());
        final Term[] terms = query.getTerms();
        final ArrayList termsLst = new ArrayList(terms.length);
        map.put("terms", termsLst);
        for (final Term term : terms) {
            final Map<String, Object> termMap = new LinkedHashMap<>();
            termMap.put(getTermField(term), term.bytes().utf8ToString());
            termsLst.add(termMap);
        }
        return map;
    }

    @Override
    public Map visit(MultiPhraseQuery query) throws Exception {
        return visitUnknownQuery(query);
    }

    @Override
    public Map visit(PrefixQuery query) throws Exception {
        final Map map = newQuery(query);
        final Term term = query.getPrefix();
        final String field = getTermField(term);
        final String termStr = term.bytes().utf8ToString();
        final String rewriteMethod = query.getRewriteMethod().getClass().getSimpleName();
        map.put(field, termStr);
        map.put("rewrite_method", rewriteMethod);
        return map;
    }

    @Override
    public Map visit(FuzzyQuery query) throws Exception {
        final Map map = newQuery(query);
        final Term term = query.getTerm();
        final String field = getTermField(term);
        final String termStr = term.bytes().utf8ToString();
        final String rewriteMethod = query.getRewriteMethod().getClass().getSimpleName();
        map.put(field, termStr);
        map.put("max_edits", query.getMaxEdits());
        map.put("prefix_length", query.getPrefixLength());
        map.put("transpositions", query.getTranspositions());
        map.put("rewrite_method", rewriteMethod);
        return map;
    }

    @Override
    public Map visit(MatchAllDocsQuery query) throws Exception {
        return visitUnknownQuery(query);
    }

    @Override
    public Map visit(ConstantScoreQuery query) throws Exception {
        final Object child = QueryAcceptor.accept(query.getQuery(), this);
        final Map map = newQuery(query);
        map.put("query", child);
        return map;
    }
}