Search in sources :

Example 1 with FuzzyLikeThisQuery

use of org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery in project lucene-solr by apache.

the class KNearestFuzzyClassifier method knnSearch.

private TopDocs knnSearch(String text) throws IOException {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    FuzzyLikeThisQuery fuzzyLikeThisQuery = new FuzzyLikeThisQuery(300, analyzer);
    for (String fieldName : textFieldNames) {
        // TODO: make this parameters configurable
        fuzzyLikeThisQuery.addTerms(text, fieldName, 1f, 2);
    }
    bq.add(fuzzyLikeThisQuery, BooleanClause.Occur.MUST);
    Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
    bq.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
    if (query != null) {
        bq.add(query, BooleanClause.Occur.MUST);
    }
    return indexSearcher.search(bq.build(), k);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term)

Example 2 with FuzzyLikeThisQuery

use of org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery in project lucene-solr by apache.

the class FuzzyLikeThisQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    NodeList nl = e.getElementsByTagName("Field");
    int maxNumTerms = DOMUtils.getAttribute(e, "maxNumTerms", DEFAULT_MAX_NUM_TERMS);
    FuzzyLikeThisQuery fbq = new FuzzyLikeThisQuery(maxNumTerms, analyzer);
    fbq.setIgnoreTF(DOMUtils.getAttribute(e, "ignoreTF", DEFAULT_IGNORE_TF));
    final int nlLen = nl.getLength();
    for (int i = 0; i < nlLen; i++) {
        Element fieldElem = (Element) nl.item(i);
        float minSimilarity = DOMUtils.getAttribute(fieldElem, "minSimilarity", DEFAULT_MIN_SIMILARITY);
        int prefixLength = DOMUtils.getAttribute(fieldElem, "prefixLength", DEFAULT_PREFIX_LENGTH);
        String fieldName = DOMUtils.getAttributeWithInheritance(fieldElem, "fieldName");
        String value = DOMUtils.getText(fieldElem);
        fbq.addTerms(value, fieldName, minSimilarity, prefixLength);
    }
    Query q = fbq;
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        q = new BoostQuery(fbq, boost);
    }
    return q;
}
Also used : FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

FuzzyLikeThisQuery (org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery)2 Query (org.apache.lucene.search.Query)2 Term (org.apache.lucene.index.Term)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 WildcardQuery (org.apache.lucene.search.WildcardQuery)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1