Guest User

Untitled

a guest
Sep 17th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintStream;
  3.  
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.document.StringField;
  7. import org.apache.lucene.document.Field.Store;
  8. import org.apache.lucene.index.DirectoryReader;
  9. import org.apache.lucene.index.IndexWriter;
  10. import org.apache.lucene.index.IndexWriterConfig;
  11. import org.apache.lucene.index.IndexableField;
  12. import org.apache.lucene.index.Term;
  13. import org.apache.lucene.search.IndexSearcher;
  14. import org.apache.lucene.search.MatchAllDocsQuery;
  15. import org.apache.lucene.search.Query;
  16. import org.apache.lucene.search.ScoreDoc;
  17. import org.apache.lucene.search.TermQuery;
  18. import org.apache.lucene.store.RAMDirectory;
  19. import org.apache.lucene.util.Version;
  20.  
  21. public class ReindexTest {
  22.     static RAMDirectory dir = new RAMDirectory();
  23.     static Version version = Version.LUCENE_4_10_0;
  24.     static IndexWriterConfig config = new IndexWriterConfig(version, new StandardAnalyzer());
  25.     static IndexWriter iw;
  26.  
  27.     static String teststring = "abc def";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         iw = new IndexWriter(dir, config);
  31.  
  32.         Document doc = new Document();
  33.         doc.add(new StringField("a", teststring, Store.YES));
  34.         doc.add(new StringField("b", "a", Store.YES));
  35.         iw.addDocument(doc);
  36.  
  37.         Term term = new Term("a", teststring);
  38.         System.out.println("Term query");
  39.         for (Document docs : search(term))
  40.             printDocument(docs, System.out);
  41.  
  42.         doc = search(term)[0];
  43.         doc.removeField("b");
  44.         doc.add(new StringField("b", "b", Store.YES));
  45.         iw.updateDocument(term, doc);
  46.        
  47.         iw.commit();
  48.         iw.waitForMerges();
  49.         iw.close();
  50.                
  51.         iw = new IndexWriter(dir, new IndexWriterConfig(version, new StandardAnalyzer()));
  52.        
  53.         System.out.println("All docs");
  54.         for (Document docs : search(new MatchAllDocsQuery()))
  55.             printDocument(docs, System.out);
  56.  
  57.         System.out.println("Term query");
  58.         for (Document docs : search(term))
  59.             printDocument(docs, System.out);
  60.     }
  61.  
  62.     public static Document[] search(Term t) throws IOException {
  63.         TermQuery q = new TermQuery(t);
  64.         return search(q);
  65.     }
  66.  
  67.     public static Document[] search(Query q) throws IOException {
  68.         DirectoryReader reader = DirectoryReader.open(iw, true);
  69.         IndexSearcher search = new IndexSearcher(reader);
  70.         ScoreDoc[] results = search.search(q, 100).scoreDocs;
  71.  
  72.         Document[] docs = new Document[results.length];
  73.  
  74.         for (int i = 0; i < docs.length; i++)
  75.             docs[i] = search.doc(results[i].doc);
  76.  
  77.         return docs;
  78.     }
  79.  
  80.     public static void printDocument(Document doc, PrintStream output) {
  81.         for (IndexableField field : doc.getFields()) {
  82.             output.println(field.toString());
  83.         }
  84.         output.println("------------------------------------");
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment