Search in sources :

Example 31 with TermRangeQuery

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

the class TestReaderClosed method test.

public void test() throws Exception {
    assertTrue(reader.getRefCount() > 0);
    IndexSearcher searcher = newSearcher(reader);
    TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
    searcher.search(query, 5);
    reader.close();
    try {
        searcher.search(query, 5);
    } catch (AlreadyClosedException ace) {
    // expected
    } catch (RejectedExecutionException ree) {
    // expected if the searcher has been created with threads since LuceneTestCase
    // closes the thread-pool in a reader close listener
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 32 with TermRangeQuery

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

the class QueryParsing method toString.

/**
   * @see #toString(Query,IndexSchema)
   */
public static void toString(Query query, IndexSchema schema, Appendable out, int flags) throws IOException {
    // clear the boosted / is clause flags for recursion
    int subflag = flags & ~(FLAG_BOOSTED | FLAG_IS_CLAUSE);
    if (query instanceof TermQuery) {
        TermQuery q = (TermQuery) query;
        Term t = q.getTerm();
        FieldType ft = writeFieldName(t.field(), schema, out, flags);
        writeFieldVal(t.bytes(), ft, out, flags);
    } else if (query instanceof TermRangeQuery) {
        TermRangeQuery q = (TermRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesLower() ? '[' : '{');
        BytesRef lt = q.getLowerTerm();
        BytesRef ut = q.getUpperTerm();
        if (lt == null) {
            out.append('*');
        } else {
            writeFieldVal(lt, ft, out, flags);
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            writeFieldVal(ut, ft, out, flags);
        }
        out.append(q.includesUpper() ? ']' : '}');
    } else if (query instanceof LegacyNumericRangeQuery) {
        LegacyNumericRangeQuery q = (LegacyNumericRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesMin() ? '[' : '{');
        Number lt = q.getMin();
        Number ut = q.getMax();
        if (lt == null) {
            out.append('*');
        } else {
            out.append(lt.toString());
        }
        out.append(" TO ");
        if (ut == null) {
            out.append('*');
        } else {
            out.append(ut.toString());
        }
        out.append(q.includesMax() ? ']' : '}');
    } else if (query instanceof BooleanQuery) {
        BooleanQuery q = (BooleanQuery) query;
        boolean needParens = false;
        if (q.getMinimumNumberShouldMatch() != 0 || (flags & (FLAG_IS_CLAUSE | FLAG_BOOSTED)) != 0) {
            needParens = true;
        }
        if (needParens) {
            out.append('(');
        }
        boolean first = true;
        for (BooleanClause c : q.clauses()) {
            if (!first) {
                out.append(' ');
            } else {
                first = false;
            }
            if (c.isProhibited()) {
                out.append('-');
            } else if (c.isRequired()) {
                out.append('+');
            }
            Query subQuery = c.getQuery();
            toString(subQuery, schema, out, subflag | FLAG_IS_CLAUSE);
        }
        if (needParens) {
            out.append(')');
        }
        if (q.getMinimumNumberShouldMatch() > 0) {
            out.append('~');
            out.append(Integer.toString(q.getMinimumNumberShouldMatch()));
        }
    } else if (query instanceof PrefixQuery) {
        PrefixQuery q = (PrefixQuery) query;
        Term prefix = q.getPrefix();
        FieldType ft = writeFieldName(prefix.field(), schema, out, flags);
        out.append(prefix.text());
        out.append('*');
    } else if (query instanceof WildcardQuery) {
        out.append(query.toString());
    } else if (query instanceof FuzzyQuery) {
        out.append(query.toString());
    } else if (query instanceof ConstantScoreQuery) {
        out.append(query.toString());
    } else if (query instanceof WrappedQuery) {
        WrappedQuery q = (WrappedQuery) query;
        out.append(q.getOptions());
        toString(q.getWrappedQuery(), schema, out, subflag);
    } else if (query instanceof BoostQuery) {
        BoostQuery q = (BoostQuery) query;
        toString(q.getQuery(), schema, out, subflag | FLAG_BOOSTED);
        out.append("^");
        out.append(Float.toString(q.getBoost()));
    } else {
        out.append(query.getClass().getSimpleName() + '(' + query.toString() + ')');
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FieldType(org.apache.solr.schema.FieldType) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 33 with TermRangeQuery

use of org.apache.lucene.search.TermRangeQuery in project tika by apache.

the class RecentFiles method generateRSS.

public String generateRSS(File indexFile) throws CorruptIndexException, IOException {
    StringBuffer output = new StringBuffer();
    output.append(getRSSHeaders());
    IndexSearcher searcher = null;
    try {
        reader = IndexReader.open(new SimpleFSDirectory(indexFile));
        searcher = new IndexSearcher(reader);
        GregorianCalendar gc = new java.util.GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
        gc.setTime(new Date());
        String nowDateTime = ISO8601.format(gc);
        gc.add(java.util.GregorianCalendar.MINUTE, -5);
        String fiveMinsAgo = ISO8601.format(gc);
        TermRangeQuery query = new TermRangeQuery(Metadata.DATE.toString(), fiveMinsAgo, nowDateTime, true, true);
        TopScoreDocCollector collector = TopScoreDocCollector.create(20, true);
        searcher.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            output.append(getRSSItem(doc));
        }
    } finally {
        if (reader != null)
            reader.close();
        if (searcher != null)
            searcher.close();
    }
    output.append(getRSSFooters());
    return output.toString();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) GregorianCalendar(java.util.GregorianCalendar) Document(org.apache.lucene.document.Document) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Date(java.util.Date) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 34 with TermRangeQuery

use of org.apache.lucene.search.TermRangeQuery in project SearchServices by Alfresco.

the class AlfrescoFieldType method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
    Analyzer multiAnalyzer = constructMultiTermAnalyzer(getQueryAnalyzer());
    BytesRef lower = analyzeMultiTerm(field.getName(), part1, multiAnalyzer);
    BytesRef upper = analyzeMultiTerm(field.getName(), part2, multiAnalyzer);
    return new TermRangeQuery(field.getName(), lower, upper, minInclusive, maxInclusive);
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) BytesRef(org.apache.lucene.util.BytesRef)

Example 35 with TermRangeQuery

use of org.apache.lucene.search.TermRangeQuery in project SearchServices by Alfresco.

the class Solr4QueryParser method addLocaleSpecificTextRange.

private void addLocaleSpecificTextRange(String expandedFieldName, PropertyDefinition pDef, String part1, String part2, boolean includeLower, boolean includeUpper, Builder booleanQuery, Locale locale, AnalysisMode analysisMode, IndexTokenisationMode tokenisationMode, IndexTokenisationMode preferredTokenisationMode) throws ParseException, IOException {
    FieldInstance fieldInstance = getFieldInstance(expandedFieldName, pDef, locale, preferredTokenisationMode);
    String firstString = null;
    if ((part1 != null) && !part1.equals("\u0000")) {
        if (fieldInstance.isLocalised()) {
            firstString = getFirstTokenForRange(getLocalePrefixedText(part1, locale), fieldInstance);
            if (firstString == null) {
                firstString = "{" + locale.getLanguage() + "}";
            }
        } else {
            firstString = getFirstTokenForRange(part1, fieldInstance);
        }
    } else {
        if (fieldInstance.isLocalised()) {
            firstString = "{" + locale.getLanguage() + "}";
        } else {
            firstString = null;
        }
    }
    String lastString = null;
    if ((part2 != null) && !part2.equals("\uffff")) {
        if (fieldInstance.isLocalised()) {
            lastString = getFirstTokenForRange(getLocalePrefixedText(part2, locale), fieldInstance);
            if (lastString == null) {
                lastString = "{" + locale.getLanguage() + "}\uffff";
            }
        } else {
            lastString = getFirstTokenForRange(part2, fieldInstance);
        }
    } else {
        if (fieldInstance.isLocalised()) {
            lastString = "{" + locale.getLanguage() + "}\uffff";
        } else {
            lastString = null;
        }
    }
    TermRangeQuery query = new TermRangeQuery(fieldInstance.getField(), firstString == null ? null : new BytesRef(firstString), lastString == null ? null : new BytesRef(lastString), includeLower, includeUpper);
    booleanQuery.add(query, Occur.SHOULD);
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

TermRangeQuery (org.apache.lucene.search.TermRangeQuery)37 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 BytesRef (org.apache.lucene.util.BytesRef)15 TermQuery (org.apache.lucene.search.TermQuery)14 Query (org.apache.lucene.search.Query)13 Term (org.apache.lucene.index.Term)10 IndexSearcher (org.apache.lucene.search.IndexSearcher)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)8 Document (org.apache.lucene.document.Document)7 BoostQuery (org.apache.lucene.search.BoostQuery)6 IndexReader (org.apache.lucene.index.IndexReader)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5 ScoreDoc (org.apache.lucene.search.ScoreDoc)5 TextField (org.apache.lucene.document.TextField)4 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)4 PhraseQuery (org.apache.lucene.search.PhraseQuery)4 PrefixQuery (org.apache.lucene.search.PrefixQuery)4 RegexpQuery (org.apache.lucene.search.RegexpQuery)4 Map (java.util.Map)3 IndexWriter (org.apache.lucene.index.IndexWriter)3