高级搜索技术–Filter

  categories:搜索资料  author:

来源:互联网

Filter顾名思义 就是用来过虑搜索结果的,在Lucene中有3个过虑器

1,DateFilter constrains the document space to only documents with a specified date field within a given range of dates.

2,QueryFilter uses the results of query as the searchable document space for a new query.

3,CachingWrapperFilter is a decorator over another filter caching its results to increase performance when used again.

import lia.common.LiaTestCase;
import org.apache.lucene.document.DateField;
005 import org.apache.lucene.index.Term;
006 import org.apache.lucene.search.BooleanQuery;
007 import org.apache.lucene.search.CachingWrapperFilter;
008 import org.apache.lucene.search.DateFilter;
009 import org.apache.lucene.search.Filter;
010 import org.apache.lucene.search.Hits;
011 import org.apache.lucene.search.IndexSearcher;
012 import org.apache.lucene.search.Query;
013 import org.apache.lucene.search.QueryFilter;
014 import org.apache.lucene.search.RangeQuery;
015 import org.apache.lucene.search.TermQuery;
016
017 import java.util.Date;
018
019 public class FilterTest extends LiaTestCase {
020   private Query allBooks;
021   private IndexSearcher searcher;
022   private int numAllBooks;
023   private CachingWrapperFilter cachingFilter;
024
025   protected void setUp() throws Exception {
026     super.setUp();
027
028     allBooks = new RangeQuery(new Term("pubmonth","190001"),
029                                    new Term("pubmonth", "200512"),
030                                    true);
031     searcher = new IndexSearcher(directory);
032     Hits hits = searcher.search(allBooks);
033     numAllBooks = hits.length();
034   }
035
036   public void testDateFilter() throws Exception {
037     Date jan1 = parseDate("2004-01-01");
038     Date jan31 = parseDate("2004-01-31");
039     Date dec31 = parseDate("2004-12-31");
040
041     DateFilter filter = new DateFilter("modified", jan1, dec31);
042
043     Hits hits = searcher.search(allBooks, filter);
044     assertEquals("all modified in 2004",
045         numAllBooks, hits.length());
046
047     filter = new DateFilter("modified", jan1, jan31);

048     hits = searcher.search(allBooks, filter);
049     assertEquals("none modified in January",
050         0, hits.length());
051   }
052
053   public void testQueryFilter() throws Exception {
054     TermQuery categoryQuery =
055        new TermQuery(new Term("category", "/philosophy/eastern"));
056
057     Filter categoryFilter = new QueryFilter(categoryQuery);////
QueryFilter
058
059     Hits hits = searcher.search(allBooks, categoryFilter);
060     assertEquals("only tao te ching", 1, hits.length());
061   }
062
063   public void testFilterAlternative() throws Exception {

064     TermQuery categoryQuery =
065        new TermQuery(new Term("category", "/philosophy/eastern"));
066
067     BooleanQuery constrainedQuery = new BooleanQuery();
068     constrainedQuery.add(allBooks, true, false);
069     constrainedQuery.add(categoryQuery, true, false);
070
071     Hits hits = searcher.search(constrainedQuery);
072     assertEquals("only tao te ching", 1, hits.length());
073   }
074
075
076   public void testQueryFilterWithRangeQuery() throws Exception {
077     Date jan1 = parseDate("2004-01-01");
078     Date dec31 = parseDate("2004-12-31");
079
080     Term start = new Term("modified",
081         DateField.dateToString(jan1));
082     Term end = new Term("modified",
083         DateField.dateToString(dec31));
084
085     Query rangeQuery = new RangeQuery(start, end, true);
086
087     Filter filter = new QueryFilter(rangeQuery);
088     Hits hits = searcher.search(allBooks, filter);
089     assertEquals("all of 'em", numAllBooks, hits.length());
090   }
091
092   public void testCachingWrapper() throws Exception {

093     Date jan1 = parseDate("2004-01-01");
094     Date dec31 = parseDate("2004-12-31");
095
096     DateFilter dateFilter =
097         new DateFilter("modified", jan1, dec31);
098
099     cachingFilter =
100         new CachingWrapperFilter(dateFilter);

101     Hits hits = searcher.search(allBooks, cachingFilter);
102     assertEquals("all of 'em", numAllBooks, hits.length());
103   }
104 }



快乐成长 每天进步一点点