You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Amit <am...@gmail.com> on 2007/08/22 15:20:59 UTC

search returns always the first indexed name

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

RE: search returns always the first indexed name

Posted by Ard Schrijvers <a....@hippo.nl>.
Use getValues("name"), see

http://lucene.zones.apache.org:8080/hudson/job/Lucene-Nightly/javadoc/org/apache/lucene/document/Document.html#getValues(java.lang.String)

Regards Ard


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