lucene-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mitbal <mit....@gmail.com>
Subject Lucene query hits always zero
Date Wed, 29 Dec 2010 03:33:29 GMT

Hi, I'm new in using lucene. I first found it to help me in my information
retrieval course. Now I'm doing my final assignment for this course using
lucene indexing.

I'm following the tutorial from this site:
http://www.lucenetutorial.com/lucene-in-5-minutes.html
to accomodate my needs, I refactor the code a bit, introducing method here
and there. Now my code looks exactly like this.

<pre>
package ir.twitraff;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class TweetIndexer {
	
	private static TweetIndexer ti;
	private StandardAnalyzer analyzer;
	private Directory index;
	private IndexWriter writer;
	private IndexSearcher searcher;
	
	private TweetIndexer() {
		try {
			analyzer = new StandardAnalyzer(Version.LUCENE_30);
			index = new RAMDirectory();
			writer = new IndexWriter(index, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
			//searcher = new IndexSearcher(index, true);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static TweetIndexer getTweetIndexer() {
		if(ti == null) {
			ti = new TweetIndexer();
		}
		return ti;
	}
	public void addTweet(String tweet) {
		try {
			addDoc(writer, tweet);
			System.out.println(tweet);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public List<String> search() throws CorruptIndexException, IOException {
		searcher = new IndexSearcher(index, true);
		List<String> list = new ArrayList<String>();
		String queryStr = "macet";
		list = search(queryStr);
		return list;
	}
	public List<String> search(String queryStr) throws CorruptIndexException,
IOException {
		searcher = new IndexSearcher(index, true);
		List<String> list = new ArrayList<String>();
		try {
			Query q = new QueryParser(Version.LUCENE_30, "title",
analyzer).parse(queryStr);
			TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);
			searcher.search(q, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;
			System.out.println(hits.length);
			for(int i=0; i<hits.length; i++) {
				int docId = hits[i].doc;
				Document d = searcher.doc(docId);
				list.add(d.get("title"));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static void main(String[] args) {
		try {
			TweetIndexer tttt = TweetIndexer.getTweetIndexer();
			tttt.addTweet("tweet 1");
			tttt.addTweet("tweet 2");
			tttt.addTweet("tweet 3");
			tttt.addTweet("tweet 1");
			tttt.addTweet("tulalit 2");
			tttt.addTweet("tatatatatata 3");
			System.out.println(tttt.index);
			System.out.println("mau diloop");
			for(String str : tttt.search("tweet")) {
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void addDoc2(IndexWriter w, String value) throws
CorruptIndexException, IOException {
		Document doc = new Document();
		doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
		w.addDocument(doc);
	}
	private static void addDoc(IndexWriter w, String value) throws
CorruptIndexException, IOException {
		Document doc = new Document();
		doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
		w.addDocument(doc);
	}
}

</pre>

But, it's result is always 0. no matter what kind of text i'm adding into
the index and no matter the query is. I'm quite desperate about this because
the deadline is near and I didn't sleep last night, figuring out what is
wrong with the code.
Is there anyone that can help me? I'm using lucene version 3.0.2

Thank you for your attention...
-- 
View this message in context: http://lucene.472066.n3.nabble.com/Lucene-query-hits-always-zero-tp2160686p2160686.html
Sent from the Lucene - General mailing list archive at Nabble.com.

Mime
View raw message