Advertisement
Guest User

Untitled

a guest
Jan 26th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  2. import org.apache.lucene.document.*;
  3. import org.apache.lucene.index.*;
  4. import org.apache.lucene.queryparser.classic.QueryParser;
  5. import org.apache.lucene.search.*;
  6. import org.apache.lucene.store.RAMDirectory;
  7. import org.apache.lucene.util.Version;
  8.  
  9. import java.io.IOException;
  10. import java.io.StringReader;
  11. import java.text.ParseException;
  12.  
  13. public class Test {
  14.  
  15.     public static void main(String[] args) throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException {
  16.         RAMDirectory idx = new RAMDirectory();
  17.         StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_41, new StringReader(""));
  18.  
  19.         IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_41, analyzer);
  20.         IndexWriter writer = new IndexWriter(
  21.                 idx,
  22.                 indexWriterConfig
  23.         );
  24.         Document d1 = new Document();
  25.         d1.add(new StringField("id", "1", Field.Store.YES));
  26.         d1.add(new Field("field", "Château Triniac Côtes du Roussillon Villages Latour Latour de France", Field.Store.YES, Field.Index.ANALYZED));
  27.         writer.addDocument(d1);
  28.  
  29.         Document d2 = new Document();
  30.         d2.add(new StringField("id", "2", Field.Store.YES));
  31.         d2.add(new Field("field", "Château Latour Beerenauslese Brand - Yountville", Field.Store.YES, Field.Index.ANALYZED));
  32.         writer.addDocument(d2);
  33.  
  34.         Document d3 = new Document();
  35.         d3.add(new StringField("id", "3", Field.Store.YES));
  36.         d3.add(new Field("field", "Château Latour Grand Vin - Pauillac", Field.Store.YES, Field.Index.ANALYZED));
  37.         writer.addDocument(d3);
  38.  
  39.         Document d4 = new Document();
  40.         d4.add(new StringField("id", "4", Field.Store.YES));
  41.         d4.add(new Field("field", "Château Latour à Pomerol", Field.Store.YES, Field.Index.ANALYZED));
  42.         writer.addDocument(d4);
  43.  
  44.         writer.commit();
  45.  
  46.         IndexReader reader = DirectoryReader.open(idx);
  47.         IndexSearcher searcher = new IndexSearcher(reader);
  48.  
  49.         Query q1 = new QueryParser(Version.LUCENE_41, "field", analyzer).parse("chateau latour");
  50.         TopDocs td1 = searcher.search(q1, 4);
  51.         System.out.println("total hits:" + td1.totalHits);
  52.         for (ScoreDoc scoreDoc : td1.scoreDocs) {
  53.             Document document = reader.document(scoreDoc.doc);
  54.             System.out.println("document id=" + document.get("id"));
  55.             System.out.println("explanation: " + searcher.explain(q1, scoreDoc.doc));
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. --- output:
  62.  
  63. total hits:4
  64. document id=4
  65. explanation: 0.060120612 = (MATCH) product of:
  66.   0.120241225 = (MATCH) sum of:
  67.     0.120241225 = (MATCH) weight(field:latour in 3) [DefaultSimilarity], result of:
  68.       0.120241225 = score(doc=3,freq=1.0 = termFreq=1.0
  69. ), product of:
  70.         0.30955842 = queryWeight, product of:
  71.           0.7768564 = idf(docFreq=4, maxDocs=4)
  72.           0.39847574 = queryNorm
  73.         0.3884282 = fieldWeight in 3, product of:
  74.           1.0 = tf(freq=1.0), with freq of:
  75.             1.0 = termFreq=1.0
  76.           0.7768564 = idf(docFreq=4, maxDocs=4)
  77.           0.5 = fieldNorm(doc=3)
  78.   0.5 = coord(1/2)
  79.  
  80. document id=1
  81. explanation: 0.053139616 = (MATCH) product of:
  82.   0.10627923 = (MATCH) sum of:
  83.     0.10627923 = (MATCH) weight(field:latour in 0) [DefaultSimilarity], result of:
  84.       0.10627923 = score(doc=0,freq=2.0 = termFreq=2.0
  85. ), product of:
  86.         0.30955842 = queryWeight, product of:
  87.           0.7768564 = idf(docFreq=4, maxDocs=4)
  88.           0.39847574 = queryNorm
  89.         0.3433253 = fieldWeight in 0, product of:
  90.           1.4142135 = tf(freq=2.0), with freq of:
  91.             2.0 = termFreq=2.0
  92.           0.7768564 = idf(docFreq=4, maxDocs=4)
  93.           0.3125 = fieldNorm(doc=0)
  94.   0.5 = coord(1/2)
  95.  
  96. document id=2
  97. explanation: 0.052605536 = (MATCH) product of:
  98.   0.10521107 = (MATCH) sum of:
  99.     0.10521107 = (MATCH) weight(field:latour in 1) [DefaultSimilarity], result of:
  100.       0.10521107 = score(doc=1,freq=1.0 = termFreq=1.0
  101. ), product of:
  102.         0.30955842 = queryWeight, product of:
  103.           0.7768564 = idf(docFreq=4, maxDocs=4)
  104.           0.39847574 = queryNorm
  105.         0.33987468 = fieldWeight in 1, product of:
  106.           1.0 = tf(freq=1.0), with freq of:
  107.             1.0 = termFreq=1.0
  108.           0.7768564 = idf(docFreq=4, maxDocs=4)
  109.           0.4375 = fieldNorm(doc=1)
  110.   0.5 = coord(1/2)
  111.  
  112. document id=3
  113. explanation: 0.052605536 = (MATCH) product of:
  114.   0.10521107 = (MATCH) sum of:
  115.     0.10521107 = (MATCH) weight(field:latour in 2) [DefaultSimilarity], result of:
  116.       0.10521107 = score(doc=2,freq=1.0 = termFreq=1.0
  117. ), product of:
  118.         0.30955842 = queryWeight, product of:
  119.           0.7768564 = idf(docFreq=4, maxDocs=4)
  120.           0.39847574 = queryNorm
  121.         0.33987468 = fieldWeight in 2, product of:
  122.           1.0 = tf(freq=1.0), with freq of:
  123.             1.0 = termFreq=1.0
  124.           0.7768564 = idf(docFreq=4, maxDocs=4)
  125.           0.4375 = fieldNorm(doc=2)
  126.   0.5 = coord(1/2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement