FAQ
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();
}

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 8 | next ›
Discussion Overview
groupjava-user @
categorieslucene
postedJun 22, '11 at 7:31a
activeJun 23, '11 at 9:46a
posts8
users3
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase