lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pranav goyal <pranavgoyal40...@gmail.com>
Subject Lucene indexing & Searching
Date Wed, 08 Jun 2011 10:27:05 GMT
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.*;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class test{

    private static String id;
    private Analyzer analyzer;

    public static void main(String args[]){


        try{
            Document d=new Document();
            File indexDir = new File("./index-dir");

            StandardAnalyzer analyzer = new
StandardAnalyzer(Version.LUCENE_31);
            IndexWriter writer = null;

            IndexWriterConfig conf = new
IndexWriterConfig(Version.LUCENE_31, analyzer);
            try {
                writer = new IndexWriter(FSDirectory.open(indexDir),conf);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            d.add(new
Field("DocId","00000341",Field.Store.YES,Field.Index.ANALYZED));
            d.add(new
Field("CockId","341",Field.Store.YES,Field.Index.ANALYZED));
            d.add(new
Field("RockId","41",Field.Store.YES,Field.Index.ANALYZED));
            writer.addDocument(d);
            writer.optimize();
            writer.close();
            IndexSearcher searcher = null;
            searcher = new IndexSearcher(FSDirectory.open(indexDir));
            QueryParser parser = new
QueryParser(Version.LUCENE_31,"text",analyzer);

            String[] queryStrings = new String[] {
"00000341","DocId","341","41",
                "9652993802" };
            TopDocs results = null;
            for (String queryString : queryStrings) {
                System.out.println("searching for: " + queryString);
                Query query1;
                query1 = parser.parse(queryString);
                results = searcher.search(query1,10);

                System.out.println("total hits: " + results.totalHits);
                ScoreDoc[] hits = results.scoreDocs;
                Document doc = null;
                for (ScoreDoc hit : hits)
                {
                    doc = searcher.doc(hit.doc);
                    System.out.printf("%5.3f %sn",hit.score,
doc.get("DocId"));
                }

            }

        }
         catch (Exception e) {
        e.printStackTrace();
        }
    }
}


This is giving search results as 0. Can someone please help me out. May be
my doubt would be silly but I am not getting what actually the problem is. I
am using Lucene 3.1

Thanks
Pranav

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message