X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/MultiFieldQueryParserWrapper.java diff --git a/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/MultiFieldQueryParserWrapper.java b/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/MultiFieldQueryParserWrapper.java new file mode 100644 index 0000000..46eb864 --- /dev/null +++ b/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/MultiFieldQueryParserWrapper.java @@ -0,0 +1,269 @@ +package org.apache.lucene.queryParser.standard; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +import java.util.Map; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.Query; + +/** + * This class behaves as the as the lucene 2.4 MultiFieldQueryParser class, but uses the new + * query parser interface instead of the old one.
+ *
+ * This class should be used when the new query parser features are needed and + * also keep at the same time the old query parser interface.
+ * + * @deprecated this class will be removed soon, it's a temporary class to be + * used along the transition from the old query parser to the new + * one + */ +@Deprecated +public class MultiFieldQueryParserWrapper extends QueryParserWrapper { + + /** + * Creates a MultiFieldQueryParser. Allows passing of a map with term to + * Boost, and the boost to apply to each term. + * + *

+ * It will, when parse(String query) is called, construct a query like this + * (assuming the query consists of two terms and you specify the two fields + * title and body): + *

+ * + * + * (title:term1 body:term1) (title:term2 body:term2) + * + * + *

+ * When setDefaultOperator(AND_OPERATOR) is set, the result will be: + *

+ * + * + * +(title:term1 body:term1) +(title:term2 body:term2) + * + * + *

+ * When you pass a boost (title=>5 body=>10) you can get + *

+ * + * + * +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0) + * + * + *

+ * In other words, all the query's terms must appear, but it doesn't matter in + * what fields they appear. + *

+ */ +public MultiFieldQueryParserWrapper(String[] fields, Analyzer analyzer, Map boosts) { + this(fields, analyzer); + StandardQueryParser qpHelper = getQueryParserHelper(); + + qpHelper.setMultiFields(fields); + qpHelper.setFieldsBoost(boosts); + + } + + /** + * Creates a MultiFieldQueryParser. + * + *

+ * It will, when parse(String query) is called, construct a query like this + * (assuming the query consists of two terms and you specify the two fields + * title and body): + *

+ * + * + * (title:term1 body:term1) (title:term2 body:term2) + * + * + *

+ * When setDefaultOperator(AND_OPERATOR) is set, the result will be: + *

+ * + * + * +(title:term1 body:term1) +(title:term2 body:term2) + * + * + *

+ * In other words, all the query's terms must appear, but it doesn't matter in + * what fields they appear. + *

+ */ + public MultiFieldQueryParserWrapper(String[] fields, Analyzer analyzer) { + super(null, analyzer); + + StandardQueryParser qpHelper = getQueryParserHelper(); + qpHelper.setAnalyzer(analyzer); + + qpHelper.setMultiFields(fields); + } + + /** + * Parses a query which searches on the fields specified. + *

+ * If x fields are specified, this effectively constructs: + * + *

+   * <code>
+   * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
+   * </code>
+   * 
+ * + * @param queries + * Queries strings to parse + * @param fields + * Fields to search on + * @param analyzer + * Analyzer to use + * @throws ParseException + * if query parsing fails + * @throws IllegalArgumentException + * if the length of the queries array differs from the length of the + * fields array + */ + public static Query parse(String[] queries, String[] fields, Analyzer analyzer) + throws ParseException { + if (queries.length != fields.length) + throw new IllegalArgumentException("queries.length != fields.length"); + BooleanQuery bQuery = new BooleanQuery(); + for (int i = 0; i < fields.length; i++) { + QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer); + Query q = qp.parse(queries[i]); + if (q != null && // q never null, just being defensive + (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) { + bQuery.add(q, BooleanClause.Occur.SHOULD); + } + } + return bQuery; + } + + /** + * Parses a query, searching on the fields specified. Use this if you need to + * specify certain fields as required, and others as prohibited. + *

+ * + *

+   * Usage:
+   * <code>
+   * String[] fields = {"filename", "contents", "description"};
+   * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
+   *                BooleanClause.Occur.MUST,
+   *                BooleanClause.Occur.MUST_NOT};
+   * MultiFieldQueryParser.parse("query", fields, flags, analyzer);
+   * </code>
+   * 
+ *

+ * The code above would construct a query: + * + *

+   * <code>
+   * (filename:query) +(contents:query) -(description:query)
+   * </code>
+   * 
+ * + * @param query + * Query string to parse + * @param fields + * Fields to search on + * @param flags + * Flags describing the fields + * @param analyzer + * Analyzer to use + * @throws ParseException + * if query parsing fails + * @throws IllegalArgumentException + * if the length of the fields array differs from the length of the + * flags array + */ + public static Query parse(String query, String[] fields, + BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException { + if (fields.length != flags.length) + throw new IllegalArgumentException("fields.length != flags.length"); + BooleanQuery bQuery = new BooleanQuery(); + for (int i = 0; i < fields.length; i++) { + QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer); + Query q = qp.parse(query); + if (q != null && // q never null, just being defensive + (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) { + bQuery.add(q, flags[i]); + } + } + return bQuery; + } + + /** + * Parses a query, searching on the fields specified. Use this if you need to + * specify certain fields as required, and others as prohibited. + *

+ * + *

+   * Usage:
+   * <code>
+   * String[] query = {"query1", "query2", "query3"};
+   * String[] fields = {"filename", "contents", "description"};
+   * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
+   *                BooleanClause.Occur.MUST,
+   *                BooleanClause.Occur.MUST_NOT};
+   * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
+   * </code>
+   * 
+ *

+ * The code above would construct a query: + * + *

+   * <code>
+   * (filename:query1) +(contents:query2) -(description:query3)
+   * </code>
+   * 
+ * + * @param queries + * Queries string to parse + * @param fields + * Fields to search on + * @param flags + * Flags describing the fields + * @param analyzer + * Analyzer to use + * @throws ParseException + * if query parsing fails + * @throws IllegalArgumentException + * if the length of the queries, fields, and flags array differ + */ + public static Query parse(String[] queries, String[] fields, + BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException { + if (!(queries.length == fields.length && queries.length == flags.length)) + throw new IllegalArgumentException( + "queries, fields, and flags array have have different length"); + BooleanQuery bQuery = new BooleanQuery(); + for (int i = 0; i < fields.length; i++) { + QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer); + Query q = qp.parse(queries[i]); + if (q != null && // q never null, just being defensive + (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) { + bQuery.add(q, flags[i]); + } + } + return bQuery; + } + +}