CustomQueryParser.java revision 986
985N/A/*
985N/A * CDDL HEADER START
985N/A *
985N/A * The contents of this file are subject to the terms of the
985N/A * Common Development and Distribution License (the "License").
985N/A * You may not use this file except in compliance with the License.
985N/A *
985N/A * See LICENSE.txt included in this distribution for the specific
985N/A * language governing permissions and limitations under the License.
985N/A *
985N/A * When distributing Covered Code, include this CDDL HEADER in each
985N/A * file and include the License file at LICENSE.txt.
985N/A * If applicable, add the following below this CDDL HEADER, with the
985N/A * fields enclosed by brackets "[]" replaced with your own identifying
985N/A * information: Portions Copyright [yyyy] [name of copyright owner]
985N/A *
985N/A * CDDL HEADER END
985N/A */
985N/A
985N/A/*
985N/A * Copyright 2010 Sun Micosystems. All rights reserved.
985N/A * Use is subject to license terms.
985N/A */
985N/A
985N/Apackage org.opensolaris.opengrok.search;
985N/A
985N/Aimport org.apache.lucene.queryParser.ParseException;
985N/Aimport org.apache.lucene.queryParser.QueryParser;
985N/Aimport org.apache.lucene.search.Query;
985N/Aimport org.opensolaris.opengrok.analysis.CompatibleAnalyser;
985N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
985N/A
985N/A/**
985N/A * A custom query parser for OpenGrok.
985N/A */
985N/Aclass CustomQueryParser extends QueryParser {
985N/A /**
985N/A * Create a query parser customized for OpenGrok.
986N/A *
986N/A * @param field default field for unqualified query terms
985N/A */
986N/A CustomQueryParser(String field) {
986N/A super(SearchEngine.LUCENE_VERSION, field, new CompatibleAnalyser());
985N/A setDefaultOperator(AND_OPERATOR);
985N/A setAllowLeadingWildcard(
985N/A RuntimeEnvironment.getInstance().isAllowLeadingWildcard());
985N/A // Convert terms to lower case manually to prevent changing the case
985N/A // if the field is case sensitive.
985N/A setLowercaseExpandedTerms(false);
985N/A }
985N/A
985N/A /**
985N/A * Is this field case sensitive?
985N/A *
985N/A * @param field name of the field to check
985N/A * @return {@code true} if the field is case sensitive,
985N/A * {@code false} otherwise
985N/A */
985N/A private static boolean isCaseSensitive(String field) {
985N/A // Only definition search and reference search are case sensitive
986N/A return QueryBuilder.DEFS.equals(field) ||
986N/A QueryBuilder.REFS.equals(field);
985N/A }
985N/A
985N/A /**
985N/A * Get a canonical form of a search term. This will convert the term
985N/A * to lower case if the field is case insensitive.
985N/A *
985N/A * @param field the field to search on
985N/A * @param term the term to search for
985N/A * @return the canonical form of the search term, which matches how it
985N/A * is stored in the index
985N/A */
986N/A // The analyzers use the default locale. They probably should have used
986N/A // a fixed locale, but since they don't, we ignore that PMD warning here.
986N/A @SuppressWarnings("PMD.UseLocaleWithCaseConversions")
985N/A private static String getCanonicalTerm(String field, String term) {
985N/A return isCaseSensitive(field) ? term : term.toLowerCase();
985N/A }
985N/A
985N/A // Override the get***Query() methods to lower case the search terms if
985N/A // the field is case sensitive. We don't need to override getFieldQuery()
985N/A // because it uses the analyzer to convert the terms to canonical form.
985N/A
985N/A @Override
985N/A protected Query getFuzzyQuery(String field, String term, float min)
985N/A throws ParseException {
985N/A return super.getFuzzyQuery(field, getCanonicalTerm(field, term), min);
985N/A }
985N/A
985N/A @Override
985N/A protected Query getPrefixQuery(String field, String term)
985N/A throws ParseException {
985N/A return super.getPrefixQuery(field, getCanonicalTerm(field, term));
985N/A }
985N/A
985N/A @Override
985N/A protected Query getRangeQuery(String field, String term1, String term2,
985N/A boolean inclusive)
985N/A throws ParseException {
985N/A return super.getRangeQuery(
985N/A field,
985N/A getCanonicalTerm(field, term1),
985N/A getCanonicalTerm(field, term2),
985N/A inclusive);
985N/A }
985N/A
985N/A @Override
985N/A protected Query getWildcardQuery(String field, String term)
985N/A throws ParseException {
985N/A return super.getWildcardQuery(field, getCanonicalTerm(field, term));
985N/A }
985N/A}