Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.*;
- import org.apache.lucene.index.*;
- import org.apache.lucene.queryparser.classic.QueryParser;
- import org.apache.lucene.search.*;
- import org.apache.lucene.store.RAMDirectory;
- import org.apache.lucene.util.Version;
- import java.io.IOException;
- import java.io.StringReader;
- import java.text.ParseException;
- public class Test {
- public static void main(String[] args) throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException {
- RAMDirectory idx = new RAMDirectory();
- StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_41, new StringReader(""));
- IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_41, analyzer);
- IndexWriter writer = new IndexWriter(
- idx,
- indexWriterConfig
- );
- Document d1 = new Document();
- d1.add(new StringField("id", "1", Field.Store.YES));
- d1.add(new Field("field", "Château Triniac Côtes du Roussillon Villages Latour Latour de France", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(d1);
- Document d2 = new Document();
- d2.add(new StringField("id", "2", Field.Store.YES));
- d2.add(new Field("field", "Château Latour Beerenauslese Brand - Yountville", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(d2);
- Document d3 = new Document();
- d3.add(new StringField("id", "3", Field.Store.YES));
- d3.add(new Field("field", "Château Latour Grand Vin - Pauillac", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(d3);
- Document d4 = new Document();
- d4.add(new StringField("id", "4", Field.Store.YES));
- d4.add(new Field("field", "Château Latour à Pomerol", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(d4);
- writer.commit();
- IndexReader reader = DirectoryReader.open(idx);
- IndexSearcher searcher = new IndexSearcher(reader);
- Query q1 = new QueryParser(Version.LUCENE_41, "field", analyzer).parse("chateau latour");
- TopDocs td1 = searcher.search(q1, 4);
- System.out.println("total hits:" + td1.totalHits);
- for (ScoreDoc scoreDoc : td1.scoreDocs) {
- Document document = reader.document(scoreDoc.doc);
- System.out.println("document id=" + document.get("id"));
- System.out.println("explanation: " + searcher.explain(q1, scoreDoc.doc));
- }
- }
- }
- --- output:
- total hits:4
- document id=4
- explanation: 0.060120612 = (MATCH) product of:
- 0.120241225 = (MATCH) sum of:
- 0.120241225 = (MATCH) weight(field:latour in 3) [DefaultSimilarity], result of:
- 0.120241225 = score(doc=3,freq=1.0 = termFreq=1.0
- ), product of:
- 0.30955842 = queryWeight, product of:
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.39847574 = queryNorm
- 0.3884282 = fieldWeight in 3, product of:
- 1.0 = tf(freq=1.0), with freq of:
- 1.0 = termFreq=1.0
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.5 = fieldNorm(doc=3)
- 0.5 = coord(1/2)
- document id=1
- explanation: 0.053139616 = (MATCH) product of:
- 0.10627923 = (MATCH) sum of:
- 0.10627923 = (MATCH) weight(field:latour in 0) [DefaultSimilarity], result of:
- 0.10627923 = score(doc=0,freq=2.0 = termFreq=2.0
- ), product of:
- 0.30955842 = queryWeight, product of:
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.39847574 = queryNorm
- 0.3433253 = fieldWeight in 0, product of:
- 1.4142135 = tf(freq=2.0), with freq of:
- 2.0 = termFreq=2.0
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.3125 = fieldNorm(doc=0)
- 0.5 = coord(1/2)
- document id=2
- explanation: 0.052605536 = (MATCH) product of:
- 0.10521107 = (MATCH) sum of:
- 0.10521107 = (MATCH) weight(field:latour in 1) [DefaultSimilarity], result of:
- 0.10521107 = score(doc=1,freq=1.0 = termFreq=1.0
- ), product of:
- 0.30955842 = queryWeight, product of:
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.39847574 = queryNorm
- 0.33987468 = fieldWeight in 1, product of:
- 1.0 = tf(freq=1.0), with freq of:
- 1.0 = termFreq=1.0
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.4375 = fieldNorm(doc=1)
- 0.5 = coord(1/2)
- document id=3
- explanation: 0.052605536 = (MATCH) product of:
- 0.10521107 = (MATCH) sum of:
- 0.10521107 = (MATCH) weight(field:latour in 2) [DefaultSimilarity], result of:
- 0.10521107 = score(doc=2,freq=1.0 = termFreq=1.0
- ), product of:
- 0.30955842 = queryWeight, product of:
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.39847574 = queryNorm
- 0.33987468 = fieldWeight in 2, product of:
- 1.0 = tf(freq=1.0), with freq of:
- 1.0 = termFreq=1.0
- 0.7768564 = idf(docFreq=4, maxDocs=4)
- 0.4375 = fieldNorm(doc=2)
- 0.5 = coord(1/2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement