Java 如何在使用Lucene编制索引时将JSON对象视为单独的文档
我有几个JSON文件,看起来像下面的那个。我想将每个文件中的每个JSON对象视为一个文档(以“user_id”作为唯一标识符)。我的代码将整个JSON文件视为一个文档。我怎样才能解决这个问题Java 如何在使用Lucene编制索引时将JSON对象视为单独的文档,java,json,lucene,Java,Json,Lucene,我有几个JSON文件,看起来像下面的那个。我想将每个文件中的每个JSON对象视为一个文档(以“user_id”作为唯一标识符)。我的代码将整个JSON文件视为一个文档。我怎样才能解决这个问题 [ { "user_id": "john_doeee", "lon": 204.0, "lat": 101.0, "stored" : true, "hashtag" : "ucriverside" }, { "user_id": "carlos_baby", "lon": 204.0, "lat": 10
[
{
"user_id": "john_doeee",
"lon": 204.0,
"lat": 101.0,
"stored" : true,
"hashtag" : "ucriverside"
},
{
"user_id": "carlos_baby",
"lon": 204.0,
"lat": 101.0,
"stored" : true,
"hashtag" : "UCR"
},
{
"user_id": "emmanuel_",
"lon": 204.0,
"lat": 101.0,
"stored" : false,
"hashtag": "riverside"
}
]
我认为这与文档方法有关?
以下是我所拥有的:
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException
{
try (InputStream stream = Files.newInputStream(file))
{
//Create lucene Document
Document doc = new Document();
doc.add(new StringField("path", file.toString(), Field.Store.YES));
doc.add(new LongPoint("modified", lastModified));
doc.add(new TextField("contents", new String(Files.readAllBytes(file)), Store.YES));
writer.updateDocument(new Term("path", file.toString()), doc);
}
}
不,这与文档方法无关。Lucene没有默认的方式来理解这是JSON文件,应该在几个Lucene文档中进行拆分。您需要使用一些JavaJSON库自己完成这项工作 许多可能性之一是将库与以下代码一起使用:
JSONArray arr = new JSONArray(" .... ");
for (int i = 0; i < arr.length(); i++) {
String text = arr.getJSONObject(i);
doc.add(new TextField("contents", text), Store.YES));
}
JSONArray arr=新的JSONArray(“…”);
对于(int i=0;i
当然,您可以自由使用任何其他JSON库,如Jackson、GSON等。感谢您的响应您是否可以通过编辑代码来使用JSON Simple?@Hana我认为这几乎超出了问题的范围