All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.srp.internal.SolrQueryParser Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2012 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.srp.internal;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;

import com.adobe.cq.social.srp.internal.AbstractSchemaMapper;

/**
 * Parse lucene queries in prep for sending them to AS/solr.
 */
public class SolrQueryParser extends QueryParser {
    private final AbstractSchemaMapper mapper;

    /**
     * Constructor.
     * @param matchVersion lucene version
     * @param f the query
     * @param a the analyzer
     * @param mapper the schema mapper
     */
    public SolrQueryParser(final AbstractSchemaMapper mapper, final Version matchVersion, final String f,
        final Analyzer a) {
        super(matchVersion, f, a);
        this.mapper = mapper;
    }

    @Override
    protected Query newFieldQuery(final Analyzer analyzer, final String field, final String queryText,
        final boolean quoted) throws ParseException {
        boolean addQuote = false;
        // if string has spaces, the WhitespaceAnalyzer will include escaped quotes already. Otherwise, we'll
        // have two escaped quotes.
        if (quoted && !StringUtils.contains(queryText, ' ')) {
            addQuote = true;
        }
        return super.newFieldQuery(analyzer, mapper.toSchemaKey(field), addQuote ? "\"" + queryText + "\""
            : queryText, quoted);
    }

    @Override
    protected Query newRangeQuery(final String field, final String part1, final String part2,
        final boolean startInclusive, final boolean endInclusive) {
        return super.newRangeQuery(mapper.toSchemaKey(field), part1, part2, startInclusive, endInclusive);
    }

    /**
     * I really didn't want to write this. I was hoping something from Lucene would manage to correct output a string
     * I could pass to Solr but it keeps ignoring quotes and escaping the wrong things.
     * @param query the query
     * @return the query string
     */
    public static String getQueryString(final Query query) {

        return query.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy