像Lucene一样在JavaScript中搜索JSON对象 [英] Lucene-like searching through JSON objects in JavaScript

查看:205
本文介绍了像Lucene一样在JavaScript中搜索JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆JSON对象(它是一个音乐库,其中包含艺术家,专辑等属性,用loadonce = true提供jqgrid)我希望通过整个集合实现类似lucene(google-like)的查询 - 但是在本地,即在浏览器中,不与Web服务器通信。是否有任何javascript框架可以帮助我?

I have a pretty big array of JSON objects (its a music library with properties like artist, album etc, feeding a jqgrid with loadonce=true) and I want to implement lucene-like (google-like) query through whole set - but locally, i.e. in the browser, without communication with web server. Are there any javascript frameworks that will help me?

推荐答案


  1. 查看您的记录,通过在名为index的单个字符串字段中组合所有搜索
    能字段来创建一次性索引。

  1. Go through your records, to create a one time index by combining all search able fields in a single string field called index.

将这些索引记录存储在数组中。

Store these indexed records in an Array.

在索引上对数组进行分区..就像一个数组中的所有数据一样,等等。

Partition the Array on index .. like all a's in one array and so on.

对索引使用javascript函数 indexOf()以匹配用户输入的查询并查找记录来自分区数组。

Use the javascript function indexOf() against the index to match the query entered by the user and find records from the partitioned Array.

这是一个简单的部分,但它将以非常有效的方式支持所有简单查询因为不必为每个查询重新创建索引,indexOf操作非常有效。我用它来搜索多达2000条记录。我使用了预先排序的数组。实际上,这就是Gmail和雅虎邮件的工作方式。它们将您的联系人存储在预先排序的数组中,其中包含一个索引,允许您在键入时查看联系人姓名。

That was the easy part but, it will support all simple queries in a very efficient manner because the index does not have to be re-created for every query and indexOf operation is very efficient. I have used it for searching up to 2000 records. I used a pre-sorted Array. Actually, that's how Gmail and yahoo mail work. They store your contacts on browser in a pre-sorted array with an index that allows you to see the contact names as you type.

这也为您提供了构建基础的基础上。现在,您可以在其上编写高级查询解析逻辑。例如,要支持一些简单的条件关键字,例如 - AND OR NOT,将需要大约20-30行自定义JavaScript代码。或者你可以找到一个JS库,它将按照Lucene的方式为你进行解析。

This also gives you a base to build on. Now you can write an advanced query parsing logic on top of it. For example, to support a few simple conditional keywords like - AND OR NOT, will take about 20-30 lines of custom JavaScript code. Or you can find a JS library that will do the parsing for you the way Lucene does.

对于上述逻辑的参考实现,看看如何 ZmContactList.js 对联系人进行排序和搜索以获得自动完成功能。

For a reference implementation of above logic, take a look at how ZmContactList.js sorts and searches the contacts for autocomplete.

这篇关于像Lucene一样在JavaScript中搜索JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆