lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pranav goyal <pranavgoyal40...@gmail.com>
Subject Re: Lucene Searching
Date Thu, 23 Jun 2011 09:08:55 GMT
Here's the code which I am implementing (Indexing and Searching codes are in
different files)

Indexing Part :

        d=new Document();
        File indexDir = new File("index-dir");
        KeywordAnalyzer analyzer = new KeywordAnalyzer();


        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_31,
analyzer);
        try {
            writer = new IndexWriter(FSDirectory.open(indexDir),conf);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String q1 = contract.getDocId();
        String q2 = contract.getDocName();
        String q3 = contract.getCustomer(ctx).getMemberName();

        Term term = new Term("DocId",contract.getDocId());
        writer.deleteDocuments(term);

        d.add(new
Field("DocId",q1,Field.Store.YES,Field.Index.NOT_ANALYZED));
        d.add(new Field("All",q2,Field.Store.NO,Field.Index.NOT_ANALYZED));
        d.add(new Field("Cust",q3,Field.Store.NO,Field.Index.NOT_ANALYZED));

        try {
            writer.addDocument(d);
            writer.close();
            endTime = System.currentTimeMillis();
            //System.out.println("Time taken to index the contract with
DocID "+q1 +" is -> " +(endTime-startTime));
        }

        catch (IOException e1) {
            e1.printStackTrace();
        }


Searching Code :

            File indexDir = new File("index-dir");
            KeywordAnalyzer analyzer = new KeywordAnalyzer();
            IndexSearcher searcher = null;

            searcher = new IndexSearcher(FSDirectory.open(indexDir));


            String[] fields = new String[] { "DocId","Item","Cust","All"};
            MultiFieldQueryParser parser = new
MultiFieldQueryParser(Version.LUCENE_31,fields,analyzer);
            parser.setAllowLeadingWildcard(true);

            String queryString = field.getValue().toString();
            TopDocs results = null;


                Query query1;
                query1 = parser.parse(queryString);
                results = searcher.search(query1,1000);


                System.out.println("total hits: " + results.totalHits);
                ScoreDoc[] hits = results.scoreDocs;
                Document doc = null;
                ArrayList docIds =  new ArrayList();
                for (ScoreDoc hit : hits)
                {
                        doc = searcher.doc(hit.doc);
                        System.out.println(doc.get("DocId"));

                        ((ArrayList) docIds).add(doc.get("DocId"));

                }
 // Function which you need not to understand
               IMnCriterion criterion =
contractQuery.createInCriterion(contractQuery.ATTR_P_DOC_ID, docIds);
               contractQuery.setCriterion(criterion);
               searcher.close();
        }

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