Java Code Examples for org.apache.lucene.search.FuzzyQuery

The following examples show how to use org.apache.lucene.search.FuzzyQuery. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public List<String> search(final String object) {
	if (stopwords.contains(object.toLowerCase())) {
		log.debug("\t Stopword detected: |" + object + "|");
		return ImmutableList.of();
	}
	ArrayList<String> uris = Lists.newArrayList();
	try {
		log.debug("\t start asking index...");

		Query q = new FuzzyQuery(new Term(FIELD_NAME_OBJECT, object), 0);
		TopScoreDocCollector collector = TopScoreDocCollector.create(numberOfDocsRetrievedFromIndex);

		isearcher.search(q, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;

		for (ScoreDoc hit : hits) {
			Document hitDoc = isearcher.doc(hit.doc);
			uris.add(hitDoc.get(FIELD_NAME_SUBJECT));
		}
		log.debug("\t finished asking index...");
	} catch (Exception e) {
		log.error(e.getLocalizedMessage() + " -> " + object, e);
	}
	return uris;
}
 
Example 2
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetFuzzyFragments() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term(FIELD_NAME, "kinnedy"), 2);
      fuzzyQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
      doSearching(fuzzyQuery);
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this, true);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 4);
    }
  };

  helper.start();
}
 
Example 3
Source Project: lucene-solr   Source File: TestSimpleQueryParser.java    License: Apache License 2.0 6 votes vote down vote up
/** test a fuzzy query */
public void testFuzzy() throws Exception {
  Query regular = new TermQuery(new Term("field", "foobar"));
  Query expected = new FuzzyQuery(new Term("field", "foobar"), 2);

  assertEquals(expected, parse("foobar~2"));
  assertEquals(expected, parse("foobar~"));
  assertEquals(regular, parse("foobar~a"));
  assertEquals(regular, parse("foobar~1a"));

  BooleanQuery.Builder bool = new BooleanQuery.Builder();
  FuzzyQuery fuzzy = new FuzzyQuery(new Term("field", "foo"), LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE);
  bool.add(fuzzy, Occur.MUST);
  bool.add(new TermQuery(new Term("field", "bar")), Occur.MUST);

  assertEquals(bool.build(), parse("foo~" + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1 + " bar"));
}
 
Example 4
Source Project: lucene-solr   Source File: TestSpans.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpanNotWithMultiterm() throws Exception {
  SpanQuery q = spanNotQuery(
      spanTermQuery(field, "r1"),
      new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "s1"))),3,3);
  checkHits(q,  new int[] {14});

  q = spanNotQuery(
      spanTermQuery(field, "r1"),
      new SpanMultiTermQueryWrapper<>(new FuzzyQuery(new Term(field, "s12"), 1, 2)),3,3);
  checkHits(q,  new int[] {14});

  q = spanNotQuery(
      new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "r"))),
      spanTermQuery(field, "s21"),3,3);
  checkHits(q,  new int[] {13});


}
 
Example 5
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 6 votes vote down vote up
public void testNoSuchMultiTermsInSpanFirst() throws Exception {
  //this hasn't been a problem  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);
 
  assertEquals(0, searcher.count(spanFirst));
  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
}
 
Example 6
Source Project: FXDesktopSearch   Source File: QueryParser.java    License: Apache License 2.0 6 votes vote down vote up
private void addToBooleanQuery(
        final List<String> aTermList, final String aFieldName, final BooleanQuery.Builder aQuery, final BooleanClause.Occur aOccour)
        throws IOException {
    for (final var theTerm : aTermList) {
        if (QueryUtils.isWildCard(theTerm)) {
            aQuery.add(new WildcardQuery(new Term(aFieldName, theTerm)), aOccour);
        } else if (QueryUtils.isFuzzy(theTerm)) {
            aQuery.add(new FuzzyQuery(new Term(aFieldName, theTerm)), aOccour);
        } else {
            final var theTokenizedTerm = toToken(theTerm, aFieldName);
            if (!StringUtils.isEmpty(theTokenizedTerm)) {
                aQuery.add(new TermQuery(new Term(aFieldName, theTokenizedTerm)), aOccour);
            }
        }
    }

}
 
Example 7
Source Project: fess   Source File: QueryHelper.java    License: Apache License 2.0 6 votes vote down vote up
protected QueryBuilder convertFuzzyQuery(final QueryContext context, final FuzzyQuery fuzzyQuery, final float boost) {
    final Term term = fuzzyQuery.getTerm();
    final String field = getSearchField(context, term.field());
    // TODO fuzzy value
    if (Constants.DEFAULT_FIELD.equals(field)) {
        context.addFieldLog(field, term.text());
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, term.text())
                .fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost));
    } else if (isSearchField(field)) {
        context.addFieldLog(field, term.text());
        return QueryBuilders.fuzzyQuery(field, term.text()).boost(boost).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits()));
    } else {
        final String origQuery = fuzzyQuery.toString();
        context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, origQuery)
                .fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost));
    }
}
 
Example 8
Source Project: crate   Source File: MatchQuery.java    License: Apache License 2.0 6 votes vote down vote up
protected Query blendTermQuery(Term term, MappedFieldType fieldType) {
    if (fuzziness != null) {
        try {
            Query query = fieldType.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, transpositions);
            if (query instanceof FuzzyQuery) {
                QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod);
            }
            return query;
        } catch (RuntimeException e) {
            if (lenient) {
                return newLenientFieldQuery(fieldType.name(), e);
            } else {
                throw e;
            }
        }
    }
    return termQuery(fieldType, term.bytes(), lenient);
}
 
Example 9
Source Project: jstarcraft-core   Source File: LuceneQueryTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFuzzyQuery() throws Exception {
    // 模糊查询
    Query query = new FuzzyQuery(new Term("title", "Stori"));
    TopDocs search = searcher.search(query, 1000);
    Assert.assertEquals(32, search.totalHits.value);
}
 
Example 10
Source Project: Elasticsearch   Source File: OptionParser.java    License: Apache License 2.0 5 votes vote down vote up
public static ParsedOptions parse(MultiMatchQueryBuilder.Type matchType,
                                  @Nullable Map options) throws IllegalArgumentException {
    if (options == null) {
        options = Collections.emptyMap();
    } else {
        // need a copy. Otherwise manipulations on a shared option will lead to strange race conditions.
        options = new HashMap(options);
    }
    ParsedOptions parsedOptions = new ParsedOptions(
            floatValue(options, OPTIONS.BOOST, null),
            analyzer(options.remove(OPTIONS.ANALYZER)),
            zeroTermsQuery(options.remove(OPTIONS.ZERO_TERMS_QUERY)),
            intValue(options, OPTIONS.MAX_EXPANSIONS, FuzzyQuery.defaultMaxExpansions),
            fuzziness(options.remove(OPTIONS.FUZZINESS)),
            intValue(options, OPTIONS.PREFIX_LENGTH, FuzzyQuery.defaultPrefixLength),
            transpositions(options.remove(OPTIONS.FUZZY_TRANSPOSITIONS))
    );

    switch (matchType.matchQueryType()) {
        case BOOLEAN:
            parsedOptions.commonTermsCutoff(floatValue(options, OPTIONS.CUTOFF_FREQUENCY, null));
            parsedOptions.operator(operator(options.remove(OPTIONS.OPERATOR)));
            parsedOptions.minimumShouldMatch(minimumShouldMatch(options.remove(OPTIONS.MINIMUM_SHOULD_MATCH)));
            break;
        case PHRASE:
            parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
            parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
            break;
        case PHRASE_PREFIX:
            parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
            parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
            parsedOptions.rewrite(rewrite(options.remove(OPTIONS.REWRITE)));
            break;
    }
    if (!options.isEmpty()) {
        raiseIllegalOptions(matchType, options);
    }
    return parsedOptions;
}
 
Example 11
Source Project: Elasticsearch   Source File: MapperQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException {
    currentFieldType = parseContext.fieldMapper(field);
    if (currentFieldType != null) {
        try {
            return currentFieldType.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength, settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
        } catch (RuntimeException e) {
            if (settings.lenient()) {
                return null;
            }
            throw e;
        }
    }
    return super.getFuzzyQuery(field, termStr, Float.parseFloat(minSimilarity));
}
 
Example 12
Source Project: Elasticsearch   Source File: MapperQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
    String text = term.text();
    int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity, text.codePointCount(0, text.length()));
    FuzzyQuery query = new FuzzyQuery(term, numEdits, prefixLength, settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
    QueryParsers.setRewriteMethod(query, settings.fuzzyRewriteMethod());
    return query;
}
 
Example 13
private List<String> getUsingFuzzySearch(String searchQuery) throws IOException {
	Query query = new FuzzyQuery(new Term(WORD_FIELD, searchQuery), 2, 0);
	List<String> foundWords = new ArrayList<>();
	TopDocs rs = searcher.search(query, 2);
	for (ScoreDoc docRef : rs.scoreDocs) {
		Document docHit = searcher.doc(docRef.doc);
		foundWords.add(docHit.get(WORD_FIELD));
	}
	return foundWords;
}
 
Example 14
@Override
public List<String> search(final String object) {
	if (stopwords.contains(object.toLowerCase())) {
		log.debug("\t Stopword detected: |" + object + "|");
		System.out.println("returning immutable empty");
		return ImmutableList.of();
	}
	ArrayList<String> uris = Lists.newArrayList();
	try {
		log.debug("\t start asking index for |" + object + "|");

		Query q = new FuzzyQuery(new Term(FIELD_NAME_OBJECT, object), 0);
		TopScoreDocCollector collector = TopScoreDocCollector.create(numberOfDocsRetrievedFromIndex);

		isearcher.search(q, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;

		for (ScoreDoc hit : hits) {
			Document hitDoc = isearcher.doc(hit.doc);
			log.debug(object + "->" + hitDoc.get(FIELD_NAME_SUBJECT) + ", " + hitDoc.get(FIELD_NAME_OBJECT));
			uris.add(hitDoc.get(FIELD_NAME_SUBJECT));
		}
		log.debug("\t finished asking index...");
	} catch (Exception e) {
		log.error(e.getLocalizedMessage() + " -> " + object, e);
	}
	return uris;
}
 
Example 15
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the minimal similarity for fuzzy queries.
 */
@Override
public float getFuzzyMinSim() {
  FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
  
  if (fuzzyConfig == null) {
    return FuzzyQuery.defaultMaxEdits;
  } else {
    return fuzzyConfig.getMinSimilarity();
  }
}
 
Example 16
Source Project: lucene-solr   Source File: StandardQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the prefix length for fuzzy queries.
 * 
 * @return Returns the fuzzyPrefixLength.
 */
@Override
public int getFuzzyPrefixLength() {
  FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
  
  if (fuzzyConfig == null) {
    return FuzzyQuery.defaultPrefixLength;
  } else {
    return fuzzyConfig.getPrefixLength();
  }
}
 
Example 17
Source Project: lucene-solr   Source File: FuzzyQueryNodeBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FuzzyQuery build(QueryNode queryNode) throws QueryNodeException {
  FuzzyQueryNode fuzzyNode = (FuzzyQueryNode) queryNode;
  String text = fuzzyNode.getTextAsString();
  
  int numEdits = FuzzyQuery.floatToEdits(fuzzyNode.getSimilarity(), 
      text.codePointCount(0, text.length()));
  
  return new FuzzyQuery(new Term(fuzzyNode.getFieldAsString(), fuzzyNode
      .getTextAsString()), numEdits, fuzzyNode
      .getPrefixLength());

}
 
Example 18
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public void testFuzzy() throws Exception {
  FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"));
  SpanQuery sfq = new SpanMultiTermQueryWrapper<>(fq);
  // will not match quick brown fox
  SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 3, 6);
  assertEquals(2, searcher.count(sprq));
}
 
Example 19
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public void testFuzzy2() throws Exception {
  // maximum of 1 term expansion
  FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"), 1, 0, 1, false);
  SpanQuery sfq = new SpanMultiTermQueryWrapper<>(fq);
  // will only match jumps over lazy broun dog
  SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 0, 100);
  assertEquals(1, searcher.count(sprq));
}
 
Example 20
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public void testNoSuchMultiTermsInNear() throws Exception {
  //test to make sure non existent multiterms aren't throwing null pointer exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanQuery near = new SpanNearQuery(new SpanQuery[]{term, spanNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  //flip order
  near = new SpanNearQuery(new SpanQuery[]{spanNoSuch, term}, 1, true);
  assertEquals(0, searcher.count(near));
  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanWCNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanRgxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

  //test single noSuch
  near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  
  //test double noSuch
  near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

}
 
Example 21
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public void testNoSuchMultiTermsInNotNear() throws Exception {
  //test to make sure non existent multiterms aren't throwing non-matching field exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanNotQuery notNear = new SpanNotQuery(term, spanNoSuch, 0,0);
  assertEquals(1, searcher.count(notNear));

  //flip
  notNear = new SpanNotQuery(spanNoSuch, term, 0,0);
  assertEquals(0, searcher.count(notNear));
  
  //both noSuch
  notNear = new SpanNotQuery(spanNoSuch, spanNoSuch, 0,0);
  assertEquals(0, searcher.count(notNear));

  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  notNear = new SpanNotQuery(term, spanWCNoSuch, 0,0);
  assertEquals(1, searcher.count(notNear));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1);
  assertEquals(1, searcher.count(notNear));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  notNear = new SpanNotQuery(term, spanPrfxNoSuch, 1, 1);
  assertEquals(1, searcher.count(notNear));
  
}
 
Example 22
Source Project: lucene-solr   Source File: TestSpanMultiTermQueryWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public void testNoSuchMultiTermsInOr() throws Exception {
  //test to make sure non existent multiterms aren't throwing null pointer exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanOrQuery near = new SpanOrQuery(new SpanQuery[]{term, spanNoSuch});
  assertEquals(1, searcher.count(near));
  
  //flip
  near = new SpanOrQuery(new SpanQuery[]{spanNoSuch, term});
  assertEquals(1, searcher.count(near));

  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanWCNoSuch});
  assertEquals(1, searcher.count(near));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanRgxNoSuch});
  assertEquals(1, searcher.count(near));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanPrfxNoSuch});
  assertEquals(1, searcher.count(near));
  
  near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch});
  assertEquals(0, searcher.count(near));
  
  near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch});
  assertEquals(0, searcher.count(near));

}
 
Example 23
Source Project: lucene-solr   Source File: SolrQueryParserBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new FuzzyQuery instance
 * @param term Term
 * @param minimumSimilarity minimum similarity
 * @param prefixLength prefix length
 * @return new FuzzyQuery Instance
 */
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
  // FuzzyQuery doesn't yet allow constant score rewrite
  String text = term.text();
  int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity,
      text.codePointCount(0, text.length()));
  return new FuzzyQuery(term,numEdits,prefixLength);
}
 
Example 24
Source Project: lucene-solr   Source File: TestExtendedDismaxParser.java    License: Apache License 2.0 5 votes vote down vote up
private boolean containsClause(Query query, String field, String value,
                               int boost, boolean fuzzy) {

  float queryBoost = 1f;
  if (query instanceof BoostQuery) {
    BoostQuery bq = (BoostQuery) query;
    query = bq.getQuery();
    queryBoost = bq.getBoost();
  }

  if(query instanceof BooleanQuery) {
    return containsClause((BooleanQuery)query, field, value, boost, fuzzy);
  }
  if(query instanceof DisjunctionMaxQuery) {
    return containsClause((DisjunctionMaxQuery)query, field, value, boost, fuzzy);
  }
  if (boost != queryBoost) {
    return false;
  }
  if(query instanceof TermQuery && !fuzzy) {
    return containsClause((TermQuery)query, field, value);
  }
  if(query instanceof FuzzyQuery && fuzzy) {
    return containsClause((FuzzyQuery)query, field, value);
  }
  return false;
}
 
Example 25
Source Project: lucene-solr   Source File: TestExtendedDismaxParser.java    License: Apache License 2.0 5 votes vote down vote up
private boolean containsClause(FuzzyQuery query, String field, String value) {
  if(query.getTerm().field().equals(field) && 
     query.getTerm().bytes().utf8ToString().equals(value)) {
    return true;
  }
  return false;
}
 
Example 26
Source Project: incubator-retired-blur   Source File: FuzzyQueryWritable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
  float boost = in.readFloat();
  TermWritable termWritable = new TermWritable();
  termWritable.readFields(in);
  Term term = termWritable.getTerm();
  int maxEdits = in.readInt();
  int prefixLength = in.readInt();
  int maxExpansions = in.readInt();
  boolean transpositions = in.readBoolean();
  query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
  query.setBoost(boost);
}
 
Example 27
Source Project: incubator-retired-blur   Source File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void assertEqualsQuery(Query expected, Query actual) {
  assertEquals(expected.getClass(), actual.getClass());
  if (expected instanceof BooleanQuery) {
    assertEqualsBooleanQuery((BooleanQuery) expected, (BooleanQuery) actual);
  } else if (expected instanceof SuperQuery) {
    assertEqualsSuperQuery((SuperQuery) expected, (SuperQuery) actual);
  } else if (expected instanceof TermQuery) {
    assertEqualsTermQuery((TermQuery) expected, (TermQuery) actual);
  } else if (expected instanceof PrefixQuery) {
    assertEqualsPrefixQuery((PrefixQuery) expected, (PrefixQuery) actual);
  } else if (expected instanceof WildcardQuery) {
    assertEqualsWildcardQuery((WildcardQuery) expected, (WildcardQuery) actual);
  } else if (expected instanceof FuzzyQuery) {
    assertEqualsFuzzyQuery((FuzzyQuery) expected, (FuzzyQuery) actual);
  } else if (expected instanceof RegexpQuery) {
    assertEqualsRegexpQuery((RegexpQuery) expected, (RegexpQuery) actual);
  } else if (expected instanceof TermRangeQuery) {
    assertEqualsTermRangeQuery((TermRangeQuery) expected, (TermRangeQuery) actual);
  } else if (expected instanceof MatchAllDocsQuery) {
    assertEqualsMatchAllDocsQuery((MatchAllDocsQuery) expected, (MatchAllDocsQuery) actual);
  } else if (expected instanceof MultiPhraseQuery) {
    assertEqualsMultiPhraseQuery((MultiPhraseQuery) expected, (MultiPhraseQuery) actual);
  } else if (expected instanceof PhraseQuery) {
    assertEqualsPhraseQuery((PhraseQuery) expected, (PhraseQuery) actual);
  } else if (expected instanceof NumericRangeQuery<?>) {
    assertEqualsNumericRangeQuery((NumericRangeQuery<?>) expected, (NumericRangeQuery<?>) actual);
  } else {
    fail("Type [" + expected.getClass() + "] not supported");
  }
}
 
Example 28
Source Project: fess   Source File: QueryHelper.java    License: Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertQuery(final QueryContext context, final Query query, final float boost) {
    if (query instanceof TermQuery) {
        return convertTermQuery(context, (TermQuery) query, boost);
    } else if (query instanceof TermRangeQuery) {
        return convertTermRangeQuery(context, (TermRangeQuery) query, boost);
    } else if (query instanceof PhraseQuery) {
        return convertPhraseQuery(context, (PhraseQuery) query, boost);
    } else if (query instanceof FuzzyQuery) {
        return convertFuzzyQuery(context, (FuzzyQuery) query, boost);
    } else if (query instanceof PrefixQuery) {
        return convertPrefixQuery(context, (PrefixQuery) query, boost);
    } else if (query instanceof WildcardQuery) {
        return convertWildcardQuery(context, (WildcardQuery) query, boost);
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery booleanQuery = (BooleanQuery) query;
        return convertBooleanQuery(context, booleanQuery, boost);
    } else if (query instanceof MatchAllDocsQuery) {
        return QueryBuilders.matchAllQuery();
    } else if (query instanceof BoostQuery) {
        final BoostQuery boostQuery = (BoostQuery) query;
        return convertQuery(context, boostQuery.getQuery(), boostQuery.getBoost());
    }
    throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY), "Unknown q: "
            + query.getClass() + " => " + query);
}
 
Example 29
Source Project: tutorials   Source File: LuceneInMemorySearchIntegrationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenFuzzyQueryWhenFetchedDocumentThenCorrect() {
    InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
    inMemoryLuceneIndex.indexDocument("article", "Halloween Festival");
    inMemoryLuceneIndex.indexDocument("decoration", "Decorations for Halloween");

    Term term = new Term("body", "hallowen");
    Query query = new FuzzyQuery(term);

    List<Document> documents = inMemoryLuceneIndex.searchIndex(query);
    Assert.assertEquals(2, documents.size());
}
 
Example 30
Source Project: crate   Source File: StringFieldType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions,
        boolean transpositions) {
    failIfNotIndexed();
    return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)),
            fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions);
}