简体   繁体   English

无法使用Apache Lucene从文档中检索字段值

[英]Unable to retrieve the field value from document using Apache Lucene

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. 您好,我是Lucene的新手,我想编写一个演示程序,该程序对一组具有唯一字段的字段进行索引,然后能够在任何字段上进行搜索。 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. 对于以下示例,名称字段返回null。

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

    }



}

Basically in your code for every field you were writing a new doc, in your previous code only description field was getting added to doc. 基本上在您编写新文档的每个字段的代码中,在以前的代码中,仅描述字段已添加到文档中。 I have updated your code to fix it:- 我已经更新了您的代码以对其进行修复:-

 import java.io.File;
    import java.io.IOException;
    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.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.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

/**
 *
 * @author user
 */
public class Indexer {
    Analyzer analyzer;
    Directory directory;
    IndexWriterConfig config;
    IndexWriter iwriter;


    final File file = new File("datasetindex");
   static public void addDoc( String field,String value, Document doc) throws IOException{
      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";

            Document doc1=new Document();
            addDoc( "id", id1,doc1);
            addDoc("name",name1,doc1);
            addDoc("description",des1,doc1);
            iwriter.addDocument(doc1);
            Document doc2=new Document();
            addDoc("id",id2,doc2);
            addDoc("name",name2,doc2);
            addDoc("description",des2,doc2);
            iwriter.addDocument(doc2);
            Document doc3=new Document();
            addDoc( "id", id3,doc3);
            addDoc( "name", name3,doc3);
            addDoc("description",des3,doc3);
            iwriter.addDocument(doc3);



            /*

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

    }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM