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

标签 java lucene

你好,我是 lucene 的新手,我想编写一个演示程序,该程序可以索引一组具有唯一字段的字段,然后能够搜索任何字段。我尝试过,但每当我打印任何未用于搜索查询的字段时,它都会返回空值。对于下面的示例,名称字段返回 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);
        } 

    }



}

最佳答案

基本上,在您编写新文档的每个字段的代码中,在之前的代码中,只有描述字段被添加到文档中。我已更新您的代码来修复它:-

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

    }



}

关于java - 无法使用 Apache Lucene 从文档中检索字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873155/

相关文章:

java - 在 Linux 中从 Java 或 Python(或其他技术驱动的东西)访问扫描器(但 Windows 会更好)

lucene - Sitecore 7 Lucene : strip HTML from computed field

java - 为什么java中一个类无法识别另一个类的getter方法?

java - 如何将java时间戳转换为php时间戳?

java - UTF-8 和 JTextArea

java - 在javascript中加载多个html文本框

java - 如何在solrj中使用Solr的所有核心

Lucene 与 Azure 搜索

python - 如何将 StandardAnalyzer 与 TermQuery 一起使用?

java - 如何避免 Apache Solr 中的 Java OutOfMemoryMemory Java 堆空间