Search in sources :

Example 1 with Filter

use of org.apache.lucene.search.Filter in project greplin-lucene-utils by Cue.

the class Filters method or.

/**
     * Returns a filter that allows documents that match any constituent
     * filter.  For convenience, null values are accepted and ignored.  If
     * all values are null, null will be returned.
     * @param filters the filters to combine
     * @return the combined filter
     */
@Nullable
public static Filter or(final Iterable<Filter> filters) {
    final BooleanFilter booleanFilter = new BooleanFilter();
    Filter lastFilter = null;
    int count = 0;
    for (Filter filter : filters) {
        if (filter != null) {
            booleanFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD));
            count += 1;
            lastFilter = filter;
        }
    }
    if (count == 0) {
        return null;
    } else if (count == 1) {
        return lastFilter;
    } else {
        return booleanFilter;
    }
}
Also used : BooleanFilter(org.apache.lucene.search.BooleanFilter) Filter(org.apache.lucene.search.Filter) BooleanFilter(org.apache.lucene.search.BooleanFilter) FilterClause(org.apache.lucene.search.FilterClause) Nullable(javax.annotation.Nullable)

Example 2 with Filter

use of org.apache.lucene.search.Filter in project greplin-lucene-utils by Cue.

the class HasFieldFilterTest method testBasics.

@Test
public void testBasics() throws Exception {
    Filter field1 = new HasFieldFilter("field1");
    Filter field2 = new HasFieldFilter("field2");
    DocIdSet hasField1 = field1.getDocIdSet(this.reader);
    Assert.assertTrue(hasField1.isCacheable());
    DocIdSet hasField2 = field2.getDocIdSet(this.reader);
    Assert.assertTrue(hasField2.isCacheable());
    assertDocIds(hasField1, true, false);
    assertDocIds(hasField2, true, true);
}
Also used : Filter(org.apache.lucene.search.Filter) DocIdSet(org.apache.lucene.search.DocIdSet) Test(org.junit.Test)

Example 3 with Filter

use of org.apache.lucene.search.Filter in project Openfire by igniterealtime.

the class ArchiveSearcher method luceneSearch.

/**
     * Searches the Lucene index for all archived conversations using the specified search.
     *
     * @param search the search.
     * @return the collection of conversations that match the search.
     */
private Collection<Conversation> luceneSearch(ArchiveSearch search) {
    try {
        IndexSearcher searcher = archiveIndexer.getSearcher();
        final StandardAnalyzer analyzer = new StandardAnalyzer();
        // Create the query based on the search terms.
        Query query = new QueryParser("text", analyzer).parse(search.getQueryString());
        // See if the user wants to sort on something other than relevance. If so, we need
        // to tell Lucene to do sorting. Default to a null sort so that it has no
        // effect if sorting hasn't been selected.
        Sort sort = null;
        if (search.getSortField() != ArchiveSearch.SortField.relevance) {
            if (search.getSortField() == ArchiveSearch.SortField.date) {
                sort = new Sort("date", search.getSortOrder() == ArchiveSearch.SortOrder.descending);
            }
        }
        // See if we need to filter on date. Default to a null filter so that it has
        // no effect if date filtering hasn't been selected.
        Filter filter = null;
        if (search.getDateRangeMin() != null || search.getDateRangeMax() != null) {
            String min = null;
            if (search.getDateRangeMin() != null) {
                min = DateTools.dateToString(search.getDateRangeMin(), DateTools.Resolution.DAY);
            }
            String max = null;
            if (search.getDateRangeMax() != null) {
                max = DateTools.dateToString(search.getDateRangeMax(), DateTools.Resolution.DAY);
            }
            // ENT-271: don't include upper or lower bound if these elements are null
            filter = new RangeFilter("date", min, max, min != null, max != null);
        }
        // See if we need to match external conversations. This will only be true
        // when less than two conversation participants are specified and external
        // wildcard matching is enabled.
        Collection<JID> participants = search.getParticipants();
        if (search.getParticipants().size() < 2 && search.isExternalWildcardMode()) {
            TermQuery externalQuery = new TermQuery(new Term("external", "true"));
            // Add this query to the existing query.
            BooleanQuery booleanQuery = new BooleanQuery();
            booleanQuery.add(query, BooleanClause.Occur.MUST);
            booleanQuery.add(externalQuery, BooleanClause.Occur.MUST);
            query = booleanQuery;
        }
        // See if we need to restrict the search to certain users.
        if (!participants.isEmpty()) {
            if (participants.size() == 1) {
                String jid = participants.iterator().next().toBareJID();
                Query participantQuery = new QueryParser("jid", analyzer).parse(jid);
                // Add this query to the existing query.
                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.add(query, BooleanClause.Occur.MUST);
                booleanQuery.add(participantQuery, BooleanClause.Occur.MUST);
                query = booleanQuery;
            } else // Otherwise there are two participants.
            {
                Iterator<JID> iter = participants.iterator();
                String participant1 = iter.next().toBareJID();
                String participant2 = iter.next().toBareJID();
                BooleanQuery participantQuery = new BooleanQuery();
                participantQuery.add(new QueryParser("jid", analyzer).parse(participant1), BooleanClause.Occur.MUST);
                participantQuery.add(new QueryParser("jid", analyzer).parse(participant2), BooleanClause.Occur.MUST);
                // Add this query to the existing query.
                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.add(query, BooleanClause.Occur.MUST);
                booleanQuery.add(participantQuery, BooleanClause.Occur.MUST);
                query = booleanQuery;
            }
        }
        Hits hits = searcher.search(query, filter, sort);
        int startIndex = search.getStartIndex();
        int endIndex = startIndex + search.getNumResults() - 1;
        // The end index can't be after the end of the results.
        if (endIndex > hits.length() - 1) {
        // endIndex = hits.length() - 1;
        // TODO: We need to determine if this is necessary.
        }
        // If the start index is positioned after the end, return an empty list.
        if (((endIndex - startIndex) + 1) <= 0) {
            return Collections.emptyList();
        } else // Otherwise return the results.
        {
            return new LuceneQueryResults(hits, startIndex, endIndex);
        }
    } catch (ParseException pe) {
        Log.error(pe.getMessage(), pe);
        return Collections.emptySet();
    } catch (IOException ioe) {
        Log.error(ioe.getMessage(), ioe);
        return Collections.emptySet();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Hits(org.apache.lucene.search.Hits) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) JID(org.xmpp.packet.JID) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) RangeFilter(org.apache.lucene.search.RangeFilter) QueryParser(org.apache.lucene.queryParser.QueryParser) RangeFilter(org.apache.lucene.search.RangeFilter) Filter(org.apache.lucene.search.Filter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Sort(org.apache.lucene.search.Sort) ParseException(org.apache.lucene.queryParser.ParseException)

Example 4 with Filter

use of org.apache.lucene.search.Filter in project querydsl by querydsl.

the class LuceneQueryTest method list_with_filter.

@Test
public void list_with_filter() {
    Filter filter = new DuplicateFilter("year");
    assertEquals(4, query.fetch().size());
    assertEquals(3, query.filter(filter).fetch().size());
}
Also used : DuplicateFilter(org.apache.lucene.sandbox.queries.DuplicateFilter) DuplicateFilter(org.apache.lucene.sandbox.queries.DuplicateFilter) Filter(org.apache.lucene.search.Filter) Test(org.junit.Test)

Example 5 with Filter

use of org.apache.lucene.search.Filter in project querydsl by querydsl.

the class LuceneQueryTest method list_with_filter.

@Test
public void list_with_filter() {
    Filter filter = new DuplicateFilter("year");
    assertEquals(4, query.fetch().size());
    assertEquals(3, query.filter(filter).fetch().size());
}
Also used : DuplicateFilter(org.apache.lucene.search.DuplicateFilter) DuplicateFilter(org.apache.lucene.search.DuplicateFilter) Filter(org.apache.lucene.search.Filter) Test(org.junit.Test)

Aggregations

Filter (org.apache.lucene.search.Filter)10 Test (org.junit.Test)6 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 Term (org.apache.lucene.index.Term)2 DuplicateFilter (org.apache.lucene.sandbox.queries.DuplicateFilter)2 BooleanFilter (org.apache.lucene.search.BooleanFilter)2 FilterClause (org.apache.lucene.search.FilterClause)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IndexingQueryFilter (org.apache.ignite.spi.indexing.IndexingQueryFilter)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 IndexReader (org.apache.lucene.index.IndexReader)1 MultiFieldQueryParser (org.apache.lucene.queryParser.MultiFieldQueryParser)1 ParseException (org.apache.lucene.queryParser.ParseException)1 QueryParser (org.apache.lucene.queryParser.QueryParser)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 DocIdSet (org.apache.lucene.search.DocIdSet)1 DuplicateFilter (org.apache.lucene.search.DuplicateFilter)1 Hits (org.apache.lucene.search.Hits)1