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

org.apache.lucene.search.PhraseQuery Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.apache.lucene.search;

/*
 * 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.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DocsAndPositionsEnum;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.similarities.Similarity.SimScorer;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.ToStringUtils;

/** A Query that matches documents containing a particular sequence of terms.
 * A PhraseQuery is built by QueryParser for input like "new york".
 * 
 * 

This query may be combined with other terms or queries with a {@link BooleanQuery}. */ public class PhraseQuery extends Query { private String field; private ArrayList terms = new ArrayList<>(4); private ArrayList positions = new ArrayList<>(4); private int maxPosition = 0; private int slop = 0; /** Constructs an empty phrase query. */ public PhraseQuery() {} /** Sets the number of other words permitted between words in query phrase. If zero, then this is an exact phrase search. For larger values this works like a WITHIN or NEAR operator.

The slop is in fact an edit-distance, where the units correspond to moves of terms in the query phrase out of position. For example, to switch the order of two words requires two moves (the first move places the words atop one another), so to permit re-orderings of phrases, the slop must be at least two.

More exact matches are scored higher than sloppier matches, thus search results are sorted by exactness.

The slop is zero by default, requiring exact matches.*/ public void setSlop(int s) { if (s < 0) { throw new IllegalArgumentException("slop value cannot be negative"); } slop = s; } /** Returns the slop. See setSlop(). */ public int getSlop() { return slop; } /** * Adds a term to the end of the query phrase. * The relative position of the term is the one immediately after the last term added. */ public void add(Term term) { int position = 0; if(positions.size() > 0) position = positions.get(positions.size()-1).intValue() + 1; add(term, position); } /** * Adds a term to the end of the query phrase. * The relative position of the term within the phrase is specified explicitly. * This allows e.g. phrases with more than one term at the same position * or phrases with gaps (e.g. in connection with stopwords). * */ public void add(Term term, int position) { if (terms.size() == 0) { field = term.field(); } else if (!term.field().equals(field)) { throw new IllegalArgumentException("All phrase terms must be in the same field: " + term); } terms.add(term); positions.add(Integer.valueOf(position)); if (position > maxPosition) maxPosition = position; } /** Returns the set of terms in this phrase. */ public Term[] getTerms() { return terms.toArray(new Term[0]); } /** * Returns the relative positions of terms in this phrase. */ public int[] getPositions() { int[] result = new int[positions.size()]; for(int i = 0; i < positions.size(); i++) result[i] = positions.get(i).intValue(); return result; } @Override public Query rewrite(IndexReader reader) throws IOException { if (terms.isEmpty()) { BooleanQuery bq = new BooleanQuery(); bq.setBoost(getBoost()); return bq; } else if (terms.size() == 1) { TermQuery tq = new TermQuery(terms.get(0)); tq.setBoost(getBoost()); return tq; } else return super.rewrite(reader); } static class PostingsAndFreq implements Comparable { final DocsAndPositionsEnum postings; final int docFreq; final int position; final Term[] terms; final int nTerms; // for faster comparisons public PostingsAndFreq(DocsAndPositionsEnum postings, int docFreq, int position, Term... terms) { this.postings = postings; this.docFreq = docFreq; this.position = position; nTerms = terms==null ? 0 : terms.length; if (nTerms>0) { if (terms.length==1) { this.terms = terms; } else { Term[] terms2 = new Term[terms.length]; System.arraycopy(terms, 0, terms2, 0, terms.length); Arrays.sort(terms2); this.terms = terms2; } } else { this.terms = null; } } @Override public int compareTo(PostingsAndFreq other) { if (docFreq != other.docFreq) { return docFreq - other.docFreq; } if (position != other.position) { return position - other.position; } if (nTerms != other.nTerms) { return nTerms - other.nTerms; } if (nTerms == 0) { return 0; } for (int i=0; i queryTerms) { queryTerms.addAll(terms); } /** Prints a user-readable version of this query. */ @Override public String toString(String f) { StringBuilder buffer = new StringBuilder(); if (field != null && !field.equals(f)) { buffer.append(field); buffer.append(":"); } buffer.append("\""); String[] pieces = new String[maxPosition + 1]; for (int i = 0; i < terms.size(); i++) { int pos = positions.get(i).intValue(); String s = pieces[pos]; if (s == null) { s = (terms.get(i)).text(); } else { s = s + "|" + (terms.get(i)).text(); } pieces[pos] = s; } for (int i = 0; i < pieces.length; i++) { if (i > 0) { buffer.append(' '); } String s = pieces[i]; if (s == null) { buffer.append('?'); } else { buffer.append(s); } } buffer.append("\""); if (slop != 0) { buffer.append("~"); buffer.append(slop); } buffer.append(ToStringUtils.boost(getBoost())); return buffer.toString(); } /** Returns true iff o is equal to this. */ @Override public boolean equals(Object o) { if (!(o instanceof PhraseQuery)) return false; PhraseQuery other = (PhraseQuery)o; return (this.getBoost() == other.getBoost()) && (this.slop == other.slop) && this.terms.equals(other.terms) && this.positions.equals(other.positions); } /** Returns a hash code value for this object.*/ @Override public int hashCode() { return Float.floatToIntBits(getBoost()) ^ slop ^ terms.hashCode() ^ positions.hashCode(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy