gpt4 book ai didi

java - 使用lucene在 native android应用程序中离线索引json文档

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:12 25 4
gpt4 key购买 nike

是否可以使用 lucene 在 native android 应用程序中离线索引文档?

我们为 web 构建它,但正在寻找可以在 native android 应用程序中离线工作的东西。

示例数据:

[{"姓名":"abc","desc":"离线索引文档"},{"名字":"jjj",“desc”:“索引我的数据”}]

我必须索引我的数据并从中搜索

分析器代码:

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

最佳答案

以此为基础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>

关于java - 使用lucene在 native android应用程序中离线索引json文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com