Search in sources :

Example 51 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project lucene-solr by apache.

the class WildcardQueryNodeBuilder method build.

@Override
public WildcardQuery build(QueryNode queryNode) throws QueryNodeException {
    WildcardQueryNode wildcardNode = (WildcardQueryNode) queryNode;
    WildcardQuery q = new WildcardQuery(new Term(wildcardNode.getFieldAsString(), wildcardNode.getTextAsString()));
    MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
    if (method != null) {
        q.setRewriteMethod(method);
    }
    return q;
}
Also used : WildcardQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.WildcardQueryNode) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Term(org.apache.lucene.index.Term)

Example 52 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project lucene-solr by apache.

the class TestPayloadScoreQuery method testRewrite.

public void testRewrite() throws IOException {
    SpanMultiTermQueryWrapper xyz = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("field", "xyz*")));
    PayloadScoreQuery psq = new PayloadScoreQuery(xyz, new AveragePayloadFunction(), false);
    // if query wasn't rewritten properly, the query would have failed with "Rewrite first!"
    searcher.search(psq, 1);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) Term(org.apache.lucene.index.Term)

Example 53 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project lucene-solr by apache.

the class SimpleNaiveBayesClassifier method countDocsWithClass.

/**
   * count the number of documents in the index having at least a value for the 'class' field
   *
   * @return the no. of documents having a value for the 'class' field
   * @throws IOException if accessing to term vectors or search fails
   */
protected int countDocsWithClass() throws IOException {
    Terms terms = MultiFields.getTerms(this.indexReader, this.classFieldName);
    int docCount;
    if (terms == null || terms.getDocCount() == -1) {
        // in case codec doesn't support getDocCount
        TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
        BooleanQuery.Builder q = new BooleanQuery.Builder();
        q.add(new BooleanClause(new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))), BooleanClause.Occur.MUST));
        if (query != null) {
            q.add(query, BooleanClause.Occur.MUST);
        }
        indexSearcher.search(q.build(), classQueryCountCollector);
        docCount = classQueryCountCollector.getTotalHits();
    } else {
        docCount = terms.getDocCount();
    }
    return docCount;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Terms(org.apache.lucene.index.Terms) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) Term(org.apache.lucene.index.Term)

Example 54 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project lucene-solr by apache.

the class KNearestNeighborDocumentClassifier method knnSearch.

/**
   * Returns the top k results from a More Like This query based on the input document
   *
   * @param document the document to use for More Like This search
   * @return the top results for the MLT query
   * @throws IOException If there is a low-level I/O error
   */
private TopDocs knnSearch(Document document) throws IOException {
    BooleanQuery.Builder mltQuery = new BooleanQuery.Builder();
    for (String fieldName : textFieldNames) {
        String boost = null;
        if (fieldName.contains("^")) {
            String[] field2boost = fieldName.split("\\^");
            fieldName = field2boost[0];
            boost = field2boost[1];
        }
        String[] fieldValues = document.getValues(fieldName);
        // we want always to use the boost coming from TF * IDF of the term
        mlt.setBoost(true);
        if (boost != null) {
            // this is an additional multiplicative boost coming from the field boost
            mlt.setBoostFactor(Float.parseFloat(boost));
        }
        mlt.setAnalyzer(field2analyzer.get(fieldName));
        for (String fieldContent : fieldValues) {
            mltQuery.add(new BooleanClause(mlt.like(fieldName, new StringReader(fieldContent)), BooleanClause.Occur.SHOULD));
        }
    }
    Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
    mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
    if (query != null) {
        mltQuery.add(query, BooleanClause.Occur.MUST);
    }
    return indexSearcher.search(mltQuery.build(), k);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StringReader(java.io.StringReader) Term(org.apache.lucene.index.Term)

Example 55 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project lucene-solr by apache.

the class SynonymTokenizer method testGetWildCardFragments.

public void testGetWildCardFragments() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            WildcardQuery wildcardQuery = new WildcardQuery(new Term(FIELD_NAME, "k?nnedy"));
            wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
            doSearching(wildcardQuery);
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 4);
        }
    };
    helper.start();
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) Term(org.apache.lucene.index.Term)

Aggregations

WildcardQuery (org.apache.lucene.search.WildcardQuery)71 Term (org.apache.lucene.index.Term)63 BooleanQuery (org.apache.lucene.search.BooleanQuery)30 PrefixQuery (org.apache.lucene.search.PrefixQuery)24 Query (org.apache.lucene.search.Query)22 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)20 TermQuery (org.apache.lucene.search.TermQuery)20 Document (org.apache.lucene.document.Document)17 BoostQuery (org.apache.lucene.search.BoostQuery)15 IndexSearcher (org.apache.lucene.search.IndexSearcher)15 RegexpQuery (org.apache.lucene.search.RegexpQuery)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 PhraseQuery (org.apache.lucene.search.PhraseQuery)14 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)14 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)14 Field (org.apache.lucene.document.Field)13 IndexReader (org.apache.lucene.index.IndexReader)13 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 TopDocs (org.apache.lucene.search.TopDocs)13 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)13