package org.apache.lucene.sandbox.queries;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.MultiTerms;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostAttribute;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.FuzzyTermsEnum;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.similarities.ClassicSimilarity;
import org.apache.lucene.search.similarities.TFIDFSimilarity;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.automaton.LevenshteinAutomata;
public class FuzzyLikeThisQuery extends Query
{
static TFIDFSimilarity sim=new ClassicSimilarity();
ArrayList<FieldVals> fieldVals=new ArrayList<>();
Analyzer analyzer;
int MAX_VARIANTS_PER_TERM=50;
boolean ignoreTF=false;
private int maxNumTerms;
@Override
public int hashCode() {
int prime = 31;
int result = classHash();
result = prime * result + Objects.hashCode(analyzer);
result = prime * result + Objects.hashCode(fieldVals);
result = prime * result + (ignoreTF ? 1231 : 1237);
result = prime * result + maxNumTerms;
return result;
}
@Override
public boolean equals(Object other) {
return sameClassAs(other) &&
equalsTo(getClass().cast(other));
}
private boolean equalsTo(FuzzyLikeThisQuery other) {
return Objects.equals(analyzer, other.analyzer) &&
Objects.equals(fieldVals, other.fieldVals) &&
ignoreTF == other.ignoreTF &&
maxNumTerms == other.maxNumTerms;
}
public FuzzyLikeThisQuery(int maxNumTerms, Analyzer analyzer)
{
this.analyzer=analyzer;
this.maxNumTerms = maxNumTerms;
}
static class FieldVals
{
String queryString;
String fieldName;
int maxEdits;
int prefixLength;
public FieldVals(String name, int maxEdits, int length, String queryString)
{
fieldName = name;
this.maxEdits = maxEdits;
prefixLength = length;
this.queryString = queryString;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((fieldName == null) ? 0 : fieldName.hashCode());
result = prime * result + maxEdits;
result = prime * result + prefixLength;
result = prime * result
+ ((queryString == null) ? 0 : queryString.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FieldVals other = (FieldVals) obj;
if (fieldName == null) {
if (other.fieldName != null)
return false;
} else if (!fieldName.equals(other.fieldName))
return false;
if (maxEdits != other.maxEdits) {
return false;
}
if (prefixLength != other.prefixLength)
return false;
if (queryString == null) {
if (other.queryString != null)
return false;
} else if (!queryString.equals(other.queryString))
return false;
return true;
}
}
public void addTerms(String queryString, String fieldName,float minSimilarity, int prefixLength)
{
int maxEdits = (int) minSimilarity;
if (maxEdits != minSimilarity || maxEdits < 0 || maxEdits > LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE) {
throw new IllegalArgumentException("minSimilarity must integer value between 0 and " + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + ", inclusive; got " + minSimilarity);
}
fieldVals.add(new FieldVals(fieldName,maxEdits,prefixLength,queryString));
}
private void addTerms(IndexReader reader, FieldVals f, ScoreTermQueue q) throws IOException {
if (f.queryString == null) return;
final Terms terms = MultiTerms.getTerms(reader, f.fieldName);
if (terms == null) {
return;
}
try (TokenStream ts = analyzer.tokenStream(f.fieldName, f.queryString)) {
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
int corpusNumDocs = reader.numDocs();
HashSet<String> processedTerms = new HashSet<>();
ts.reset();
while (ts.incrementToken()) {
String term = termAtt.toString();
if (!processedTerms.contains(term)) {
processedTerms.add(term);
ScoreTermQueue variantsQ = new ScoreTermQueue(MAX_VARIANTS_PER_TERM);
float minScore = 0;
Term startTerm = new Term(f.fieldName, term);
FuzzyTermsEnum fe = new FuzzyTermsEnum(terms, startTerm, f.maxEdits, f.prefixLength, true);
int df = reader.docFreq(startTerm);
int numVariants = 0;
int totalVariantDocFreqs = 0;
BytesRef possibleMatch;
BoostAttribute boostAtt =
fe.attributes().addAttribute(BoostAttribute.class);
while ((possibleMatch = fe.next()) != null) {
numVariants++;
totalVariantDocFreqs += fe.docFreq();
float score = boostAtt.getBoost();
if (variantsQ.size() < MAX_VARIANTS_PER_TERM || score > minScore) {
ScoreTerm st = new ScoreTerm(new Term(startTerm.field(), BytesRef.deepCopyOf(possibleMatch)), score, startTerm);
variantsQ.insertWithOverflow(st);
minScore = variantsQ.top().score;
}
fe.setMaxNonCompetitiveBoost(variantsQ.size() >= MAX_VARIANTS_PER_TERM ? minScore : Float.NEGATIVE_INFINITY);
}
if (numVariants > 0) {
int avgDf = totalVariantDocFreqs / numVariants;
if (df == 0)
{
df = avgDf;
}
int size = variantsQ.size();
for (int i = 0; i < size; i++) {
ScoreTerm st = variantsQ.pop();
st.score = (st.score * st.score) * sim.idf(df, corpusNumDocs);
q.insertWithOverflow(st);
}
}
}
}
ts.end();
}
}
private Query newTermQuery(IndexReader reader, Term term) throws IOException {
if (ignoreTF) {
return new ConstantScoreQuery(new TermQuery(term));
} else {
TermStates context = new TermStates(reader.getContext());
for (LeafReaderContext leafContext : reader.leaves()) {
Terms terms = leafContext.reader().terms(term.field());
if (terms != null) {
TermsEnum termsEnum = terms.iterator();
if (termsEnum.seekExact(term.bytes())) {
int freq = 1 - context.docFreq();
context.register(termsEnum.termState(), leafContext.ord, freq, freq);
}
}
}
return new TermQuery(term, context);
}
}
@Override
public void visit(QueryVisitor visitor) {
visitor.visitLeaf(this);
}
@Override
public Query rewrite(IndexReader reader) throws IOException
{
ScoreTermQueue q = new ScoreTermQueue(maxNumTerms);
for (FieldVals f : fieldVals) {
addTerms(reader, f, q);
}
BooleanQuery.Builder bq = new BooleanQuery.Builder();
HashMap<Term,ArrayList<ScoreTerm>> variantQueries=new HashMap<>();
int size = q.size();
for(int i = 0; i < size; i++)
{
ScoreTerm st = q.pop();
ArrayList<ScoreTerm> l= variantQueries.get(st.fuzziedSourceTerm);
if(l==null)
{
l=new ArrayList<>();
variantQueries.put(st.fuzziedSourceTerm,l);
}
l.add(st);
}
for (Iterator<ArrayList<ScoreTerm>> iter = variantQueries.values().iterator(); iter.hasNext();)
{
ArrayList<ScoreTerm> variants = iter.next();
if(variants.size()==1)
{
ScoreTerm st= variants.get(0);
Query tq = newTermQuery(reader, st.term);
bq.add(new BoostQuery(tq, st.score), BooleanClause.Occur.SHOULD);
}
else
{
BooleanQuery.Builder termVariants=new BooleanQuery.Builder();
for (Iterator<ScoreTerm> iterator2 = variants.iterator(); iterator2
.hasNext();)
{
ScoreTerm st = iterator2.next();
Query tq = newTermQuery(reader, st.term);
termVariants.add(new BoostQuery(tq, st.score), BooleanClause.Occur.SHOULD);
}
bq.add(termVariants.build(), BooleanClause.Occur.SHOULD);
}
}
return bq.build();
}
private static class ScoreTerm{
public Term term;
public float score;
Term fuzziedSourceTerm;
public ScoreTerm(Term term, float score, Term fuzziedSourceTerm){
this.term = term;
this.score = score;
this.fuzziedSourceTerm=fuzziedSourceTerm;
}
}
private static class ScoreTermQueue extends PriorityQueue<ScoreTerm> {
public ScoreTermQueue(int size){
super(size);
}
@Override
protected boolean lessThan(ScoreTerm termA, ScoreTerm termB) {
if (termA.score== termB.score)
return termA.term.compareTo(termB.term) > 0;
else
return termA.score < termB.score;
}
}
@Override
public String toString(String field)
{
return null;
}
public boolean isIgnoreTF()
{
return ignoreTF;
}
public void setIgnoreTF(boolean ignoreTF)
{
this.ignoreTF = ignoreTF;
}
}