title: Unable to retrieve the field value from document using Apache Lucene
body: <p>Hello I'm new to lucene I want to write a demo program that index a group of fields having a unique field, then been able to search on any of the fields. I tried but its returning a null value whenever I printed any of the field that was not used for the search query. the name field is returning null for the example below.</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.nio.file.Path;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  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.document.StringField;
  import org.apache.lucene.document.TextField;
  import org.apache.lucene.index.DirectoryReader;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.index.IndexWriterConfig;
  import org.apache.lucene.index.Term;
  import org.apache.lucene.queryparser.classic.ParseException;
  import org.apache.lucene.queryparser.classic.QueryParser;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.Query;
  import org.apache.lucene.search.ScoreDoc;
  import org.apache.lucene.search.TermQuery;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.FSDirectory;
  import org.apache.lucene.store.RAMDirectory;
  
  /**
   *
   * @author user
   */
  public class Indexer {
      Analyzer analyzer;
      Directory directory;
      IndexWriterConfig config;
      IndexWriter iwriter;
      static Document doc;
  
      final File file = new File("datasetindex");
     static public void addDoc(IndexWriter w, String field,String value) throws IOException{
         doc = new Document();
        if("id".equals(field)){
        doc.add(new Field(field,value, Field.Store.YES,Field.Index.NOT_ANALYZED));
        }else{
        doc.add(new Field(field,value, TextField.TYPE_STORED));
        }
  
       // w.addDocument(doc);
              }
      public static void main(String args[]){
  
  
          try {
              Analyzer analyzer = new StandardAnalyzer();
  
              Directory directory = new RAMDirectory();
              IndexWriterConfig config = new IndexWriterConfig(analyzer);
              config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
              IndexWriter iwriter = new IndexWriter(directory, config);
  
              String id1 = "1";
              String name1 = "WAN (Wireless Area network)";
              String des1 = "large network, used for country or continent,make use of satelite and gave rise to broadband network";
  
              String id2 = "2";
              String name2 = "MAN (Metropolitan Area network)";
              String des2 = "large networks usually used for cities and intranet networks";
  
              String id3 = "3";
              String name3 = "LAN (local area network)";
              String des3 = "connection between computers over small land mass";
  
  
              addDoc(iwriter, "id", id1);
              addDoc(iwriter,"name",name1);
              addDoc(iwriter,"description",des1);
              iwriter.addDocument(doc);
              addDoc(iwriter,"id",id2);
              addDoc(iwriter,"name",name2);
              addDoc(iwriter,"description",des2);
              iwriter.addDocument(doc);
              addDoc(iwriter, "id", id3);
              addDoc(iwriter, "name", name3);
              addDoc(iwriter,"description",des3);
              iwriter.addDocument(doc);
  
  
  
              /*
  
              addDoc(iwriter,"name",name3);
              addDoc(iwriter,"description",des1);
              addDoc(iwriter,"description",des2);
              addDoc(iwriter,"description",des3);
                      */
  
              iwriter.close();
  
              //searching started
              DirectoryReader ireader = DirectoryReader.open(directory);
              IndexSearcher isearcher = new IndexSearcher(ireader);
              //parse a search query
              TermQuery tq = new TermQuery(new Term("description", "networks"));
              //QueryParser parser = new QueryParser("description", analyzer);
              //Query query = parser.parse("country");
  
              ScoreDoc hits[] = isearcher.search(tq,1000).scoreDocs;
              System.out.println("Records found: "+hits.length);
              for(int a=0; a &lt; hits.length; a++){
              Document HitDoc = isearcher.doc(hits[a].doc);
              System.out.println("A DOC: "+HitDoc.get("name"));
              System.out.println("DOC ID: "+hits[a].doc);
              }
              ireader.close();
              directory.close();
  
          } catch (IOException ex) {
              Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
          } 
  
      }
  
  
  
  }
  </code></pre>