使用Lucene在原生Android应用程序中离线索引JSON文档

nsc4cvqm  于 2023-08-05  发布在  Lucene
关注(0)|答案(2)|浏览(268)

有没有可能使用lucene在原生android应用程序中离线索引文档?
我们建立它的网络,但寻找的东西,将在本地Android应用程序离线工作。
样本数据:
[ {“name”:“abc”,“desc”:“ndex documents offline”},{“name”:“jjj”,“desc”:“index my data”} ]
我必须索引我的数据并从中搜索
分析仪代码:

//  Directory dir = FSDirectory.open("/libs/g");
//Analyzer analyzer = new StandardAnalyzer();
// IndexWriterConfig iwc = new IndexWriterConfig();
// Analyzer analyzer = new StandardAnalyzer();

Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
//Directory directory = FSDirectory.open("/tmp/testindex");
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4,new Analyzer(){

protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
    return null;
    }
});
config.setOpenMode(OpenMode.CREATE_OR_APPEND);
indexWriter = new IndexWriter(directory, config);

//Always overwrite the directory
//iwriter.setOpenMode(OpenMode.CREATE);
//indexWriter = new IndexWriter(dir, iwc);

字符串

aemubtdh

aemubtdh1#

使用此作为基础http://www.avajava.com/tutorials/lessons/how-do-i-use-lucene-to-index-and-search-text-files.html
并使用它将JSON对象添加到索引

public void addDocuments(IndexWriter indexWriter, JSONArray jsonObjects) {
    for (JSONObject object : (List<JSONObject>) jsonObjects) {
        Document doc = new Document();
        final FieldType bodyOptions = new FieldType();
        bodyOptions.setIndexed(true);
        bodyOptions.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
        bodyOptions.setStored(true);
        bodyOptions.setStoreTermVectors(true);
        bodyOptions.setTokenized(true);
        for (String field : (Set<String>) object.keySet()) {
            doc.add(new Field(field, (String) object.get(field), bodyOptions));
        }
        try {
            System.out.println(doc);
            indexWriter.addDocument(doc);
        } catch (IOException ex) {
            System.err.println("Error adding documents to the index. " + ex.getMessage());
        }
    }
}

字符串
您可能需要添加以下依赖项

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-analyzers-common</artifactId>
    <version>4.10.4</version>
</dependency>

flmtquvp

flmtquvp2#

您可以使用Lucene 7.3.0的此修改在Android 8.0或更高版本中进行搜索:https://github.com/texophen/lucene-android
请参阅此答案(https://stackoverflow.com/a/76719477/22251021)了解如何使用它。
要在Android中使用Lucene,您可以:

  • 扩展org.apache.lucene.store.BaseDirectory:
package com.texopher.gophoxes;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.apache.lucene.store.BaseDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.LockFactory;

public class BucketDirectory extends BaseDirectory {

    protected Bucket bucket;
    
    public BucketDirectory(Bucket bucket, LockFactory lockFactory) {
        super(lockFactory);
        this.bucket = bucket;
    }

    @Override
    public void close() throws IOException {
    }

    @Override
    public IndexOutput createOutput(String name, IOContext context) throws IOException {
        return new BucketIndexOutput(bucket, name);
    }

    @Override
    public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException {
        return new BucketIndexOutput(bucket, prefix + UUID.randomUUID().toString().replaceAll("-", "") + suffix);
    }

    @Override
    public void deleteFile(String name) throws IOException {
        try {
            bucket.deleteFile(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public long fileLength(String name) throws IOException {
        try {
            return bucket.fileLength(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public String[] listAll() throws IOException {
        String[] tag = new String[0];
        try {
            List<String> tmp = bucket.listFiles();
            tag = new String[tmp.size()];
            for (int i = 0; i < tmp.size(); i++) {
                tag[i] = tmp.get(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tag;
    }

    @Override
    public IndexInput openInput(String name, IOContext context) throws IOException {
        try {
            return new BucketIndexInput(bucket, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void rename(String source, String dest) throws IOException {
        try {
            bucket.rename(source, dest);;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sync(Collection<String> arg0) throws IOException {
    }

    @Override
    public void syncMetaData() throws IOException {
    }

}

字符串

  • 使用以下代码进行搜索:
Query qr = new TermQuery(new Term("code", this.md5(link)));
Bucket bk = new Bucket(this.gopherServer, this.hole, this.magic);
Directory indexDirectory = new BucketDirectory(bk, new SingleInstanceLockFactory());
IndexReader reader = DirectoryReader.open(indexDirectory);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(qr, 1);
ScoreDoc[] hits = docs.scoreDocs;

相关问题