自定义Filter,实现过滤

package com.xp.luence.test;

import java.io.IOException;
import java.util.BitSet;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.Filter;

public class SecurityFilter extends Filter {
 private static String title = "title";
 private static String content = "content";
 private static String security = "security";
 public static String titleStore;
 public static String contentStore;
 
 public BitSet bits(IndexReader reader) throws IOException {

  BitSet bitSet = new BitSet(reader.maxDoc());
  bitSet.set(0, reader.maxDoc() - 1);// 设置位队列的每一位都问TRUE
  
  Term term = new Term(security, SecurityFilterSearcher.advance);
  TermDocs termDocs = reader.termDocs(term);// 从索引中取出含term的文档

  // 遍历文档,设置相应的BitSet中的值问false
  while (termDocs.next()) {
   bitSet.set(termDocs.doc(), false);
  }
  return bitSet;
 }

}

 

 

 

package com.xp.luence.test;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;

public class SecurityFilterSearcher {
 public static String advance = "advance";
 /**
  * @param args
  */
 public static void main(String[] args) {
  RAMDirectory directory = new RAMDirectory();
  String path = "F:\\directory";
  try {
   IndexWriter writer = new IndexWriter(path,
     new StandardAnalyzer(), true, MaxFieldLength.UNLIMITED);
   Document document1 = new Document();
   Field f1 = new Field("content", "advance one content key 1",
     Field.Store.YES, Field.Index.ANALYZED);
   Field f2 = new Field("security", "advance", Field.Store.YES,
     Field.Index.NOT_ANALYZED);
   document1.add(f1);
   document1.add(f2);
   Document document2 = new Document();
   Field f3 = new Field("content", "normal content key 1",
     Field.Store.YES, Field.Index.ANALYZED);
   Field f4 = new Field("security", "normal", Field.Store.YES,
     Field.Index.NOT_ANALYZED);
   document2.add(f3);
   document2.add(f4);
   Document document3 = new Document();
   Field f5 = new Field("content", "advance two content key 1",
     Field.Store.YES, Field.Index.ANALYZED);
   Field f6 = new Field("security", "advance", Field.Store.YES,
     Field.Index.NOT_ANALYZED);
   document3.add(f5);
   document3.add(f6);
   writer.addDocument(document1);
   writer.addDocument(document2);
   writer.addDocument(document3);

   writer.close();
  } catch (CorruptIndexException e) {
   e.printStackTrace();
  } catch (LockObtainFailedException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

  try {
   IndexReader reader = IndexReader.open(path);
   IndexSearcher searcher = new IndexSearcher(reader);
   TermQuery query = new TermQuery(new Term("content", "one"));
   SecurityFilter filter = new SecurityFilter();
   Hits hits = searcher.search(query, filter);// 在搜索时应用过滤器
   for (int i = 0; i < hits.length(); i++) {
    System.out.println(hits.doc(i).get("content"));
   }

  } catch (CorruptIndexException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

 

上面的SecurityFilter是自己定义lucene的过滤器。实现过滤索引中的相应的内容。代码可以直接运行看到效果!

你可能感兴趣的:(apache,Security,XP,F#,Lucene)