简体   繁体   English

将lon对象存储到lucene索引中或从中检索Json对象

[英]Storing and retrieving Json object to/from lucene indexes

I have store a set of json object into the lucene indexes and also want to retrieve it from the index. 我已经将一组json对象存储到lucene索引中,并且还希望从索引中检索它。 I am using lucene-3.4. 我正在使用lucene-3.4。

So is there any library or easy mechanism to make this happen in lucene. 那么在lucene中是否有任何库或简单的机制来实现这一点。

For sample: Json object 对于示例:Json对象

{
    BOOKNAME1: {
        id:1,
        name:"bname1",
        price:"p1"
    },
    BOOKNAME2: {
        id:2,
        name:"bname2",
        price:"p2"
    },
    BOOKNAME3: {
        id:3,
        name:"bname3",
        price:"p3"
    }
}

Any sort of help will be appreciated. 任何形式的帮助将不胜感激。 Thanks in advance, 提前致谢,

I would recommend you to index your json object by: 我建议你通过以下方式索引你的json对象:

1) Parse your json file. 1)解析你的json文件。 I usually use json simple . 我通常使用json简单

2) Open an index using IndexWriterConfig 2)使用IndexWriterConfig打开索引

3) Add documents to the index. 3)将文档添加到索引中。

4) Commit changes and close the index 4)提交更改并关闭索引

5) Run your queries 5)运行您的查询

If you would like to use Lucene Core instead of elasticsearch, I have created a sample project, which gets as an input a file with JSON objects and creates an Index. 如果你想使用Lucene Core而不是elasticsearch,我已经创建了一个示例项目,它将一个带有JSON对象的文件作为输入,并创建一个Index。 Also, I have added a test to query the index. 另外,我添加了一个测试来查询索引。

I am using the latest Lucene version (4.8), please have a look here: 我使用的是最新的Lucene版本(4.8),请看这里:

http://ignaciosuay.com/getting-started-with-lucene-and-json-indexing/ http://ignaciosuay.com/getting-started-with-lucene-and-json-indexing/

If you have time, I think it is worth reading "Lucene in Action". 如果你有时间,我认为值得阅读“Lucene in Action”。

Hope it helps. 希望能帮助到你。

If you don't want to search within the json but only store it, you just need to extract the id, which will hopefully be unique. 如果你不想在json中搜索但只存储它,你只需要提取id,这有望是唯一的。 Then your lucene document would have two fields: 然后您的lucene文档将有两个字段:

  • the id (indexed, not necessarily stored) id(索引,不一定存储)
  • the json itself, as it is (only stored) json本身,因为它(仅存储)

Once you stored your json in lucene you can retrieve it filtering by id. 一旦将json存储在lucene中,就可以通过id检索它。

On the other hand this is pretty much what elasticsearch does with your documents. 另一方面,这几乎是elasticsearch对您的文档所做的。 You just send some json to it via a REST api. 你只需通过REST api向它发送一些json。 elasticsearch will keep the json as it is and also make it searchable by default. elasticsearch将保持json不变,并且默认情况下也可以搜索它。 That means you can either retrieve the json by id or search against it, out of the box without having to write any code. 这意味着你可以通过id检索json或者搜索它,开箱即用而无需编写任何代码。

Also, with lucene your documents wouldn't be available till you commit your documents or reopen the index reader, while elasticsearch adds a handy transaction log to it, so that the GET is always real time. 此外,使用lucene,在提交文档或重新打开索引阅读器之前,您的文档将无法使用,而elasticsearch会为其添加一个方便的事务日志,因此GET始终是实时的。

Also, elasticsearch offers a lot more: a nice distributed infrastructure, faceting, scripting and more. 此外,elasticsearch提供了更多:良好的分布式基础架构,分面,脚本等。 Check it out! 看看这个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM