Results 1 to 1 of 1
Thread: o hits while using pharsequery
- 04-13-2010, 09:46 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
o hits while using pharsequery
import java.io.IOException;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyz er;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
public class PharseQuery {
public static void main(String[] args) throws IOException, ParseException {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new //StandardAnalyzer(Version.LUCENE_CURRENT);
WhitespaceAnalyzer wanalyzer= new WhitespaceAnalyzer();
// 1. create the index
Directory index = new RAMDirectory();
// the boolean arg in the IndexWriter ctor means to
// create a new index, overwriting any existing index
IndexWriter w = new IndexWriter(index, wanalyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
addDoc(w, "LuceneinAction");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "The Art of Computer Science");
w.close();
String str ="Lucene in Action";
String[] phrase = str.toLowerCase().split(" ");
BooleanQuery bQuery = new BooleanQuery();
PhraseQuery pQuery = new PhraseQuery();
for (int i = 0; i < phrase.length; i++) {
System.out.println("phase: " +phrase[i] );
pQuery.add(new Term("name", phrase[i]));
}
pQuery.setSlop(1);
bQuery.add(pQuery, BooleanClause.Occur.MUST);
// 3. search
int hitsPerPage = 10;
IndexSearcher searcher = new IndexSearcher(index, true);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
System.out.println("collector" + collector);
searcher.search(bQuery, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("name"));
}
// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}
private static void addDoc(IndexWriter w, String value) throws IOException {
Document doc = new Document();
doc.add(new Field("name", value, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
Wghen I execute this program i get 0 hits can anybody suggest me what I am missing.
Thanks in advance
Smitha
Similar Threads
-
Measuring hits for words in a document.
By bodrulalam in forum LuceneReplies: 0Last Post: 02-20-2010, 05:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks