Hi
I am using lucene to index xml. I have already managed to index the
elements. I am indexing the <name> element of xml which have multiple <name>
elements in a document.
The code is as follows
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer();
Rules.mainnn();
List<String> names = Rules.getNames();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
Document contactDocument = new Document();
for(String s:names){
contactDocument.add(new Field("name", s, Field.Store.YES,
Field.Index.TOKENIZED));
}
writer.addDocument(contactDocument);
writer.close();
Searcher searcher = new IndexSearcher(directory);
String queryString = "level";
QueryParser parser = new QueryParser("name", new
StandardAnalyzer());
Query query = parser.parse(queryString);
Hits hits = searcher.search(query);
int hitCount = hits.length();
if (hitCount == 0)
{
System.out.println("No matches were found for \"" + queryString +
"\"");
}
else
{
System.out.println("Hits for \"" + queryString
+ "\" were found in");
for (int i = 0; i < hitCount; i++)
{
Document doc = hits.doc(i);
System.out.println(" " + (i + 1) + ". " + doc.get("name"));
}
}
what I expect from the search is to give me the name of the string I am
storing with respect to the search string, but everytime I search for a
string the doc.get("name") always returns the first stored string as name. I
dont understand where have I done wrong, or should I use a different
approach..
Any help appreciated.
Cheers
Amit