Lucene Document和Java ArrayList

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucene Document和Java ArrayList相关的知识,希望对你有一定的参考价值。

我有一组数据:ID,Name和与ID关联的ArrayList。我希望这些数据存储在Lucene文档中。搜索将基于Id和名称。列表不应编入索引。

我不知道List / ArrayList如何存储在Lucene文档中。这样做的最佳方法是什么?

我使用的Apache Lucene verison是7.1.0。

谢谢。

Document doc = new Document(); String id =“something”; String name =“someName”; 列出someList = new ArrayList(); doc.add(new StringField(“Id”,id,Field.Store.YES)); doc.add(new TextField(“name”,name,Field.Store.YES));

我如何为'someList'做类似的事情?

答案

你可以使用StoredField

一种简单的方法可能是序列化列表。这是一个示例代码:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;

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.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class LuceneTest {

    public static byte[] serialize(Object obj) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(200000);
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(obj);
        os.close();
        return bos.toByteArray();
    }

    public static Object unserialize(byte[] bytes) throws ClassNotFoundException, IOException {
        ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object result = is.readObject();
        is.close();
        return result;
    }

    public static void index() {
        String indexPath = "index";
        try {
            System.out.println("Indexing to directory '" + indexPath + "'...");
            Directory dir = FSDirectory.open(Paths.get(indexPath));
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

            iwc.setOpenMode(OpenMode.CREATE);

            IndexWriter writer = new IndexWriter(dir, iwc);

            for (int i = 0; i < 100; i++) {
                Document doc = new Document();
                String id = "id" + i;
                String name = "jack " + i;
                ArrayList<Object> someList = new ArrayList<>();
                someList.add(id + " => " + name);
                someList.add("index" + i);

                doc.add(new StringField("Id", id, Field.Store.YES));
                doc.add(new TextField("name", name, Field.Store.YES));
                doc.add(new StoredField("list", serialize(someList)));
                writer.addDocument(doc);
            }

            writer.close();
        } catch (IOException e) {
            System.out.println(" caught a " + e.getClass() + "
 with message: " + e.getMessage());
        }
    }

    public static void search() {
        String index = "index";
        String field = "name";
        int hitsPerPage = 10;
        try {
            IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
            IndexSearcher searcher = new IndexSearcher(reader);
            Analyzer analyzer = new StandardAnalyzer();

            QueryParser parser = new QueryParser(field, analyzer);

            Random rnd = new Random();
            for (int i = 0; i < 10; i++) {
                int randIndex = rnd.nextInt(100);

                String nameQuery = "" + randIndex;
                Query query = parser.parse(nameQuery);
                System.out.println("Searching for: " + query.toString(field));

                TopDocs results = searcher.search(query, hitsPerPage);
                ScoreDoc[] hits = results.scoreDocs;

                for (ScoreDoc scoreDoc : hits) {
                    Document doc = searcher.doc(scoreDoc.doc);
                    String id = doc.get("Id");
                    String name = doc.get("name");
                    ArrayList<Object> list = (ArrayList<Object>) unserialize(doc.getBinaryValue("list").bytes);
                    System.out.println("id: " + id + " name: " + name + " list: " + list);
                }
                System.out.println();
            }
            reader.close();
        } catch (Exception e) {
            System.out.println(" caught a " + e.getClass() + "
 with message: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        index();
        search();
    }
}

以上是关于Lucene Document和Java ArrayList的主要内容,如果未能解决你的问题,请参考以下文章

Lucene(7.3.1)学习笔记-Document类源码解析

Lucene系列:搜索结果摘要

Lucene系列:搜索关键字高亮

Lucene中的Document

5 统计solr目录索引信息

lucene中创建索引库