用于搜索引擎样式搜索的JavaScript库? [英] JavaScript library for search engine style searching?
问题描述
是否有一个JavaScript库可以确定字符串是否与搜索查询匹配?它应该是高效的,并提供类似Google或LexisNexis的高级查询功能(诸如和/或运算符,同义词和括号之类的东西)。任何类型的高级搜索功能都会很棒;它不必与任何特定的搜索引擎完全匹配。
Is there a JavaScript library that can determine if a string matches a search query? It should be efficient and provide advanced query functionality like that of Google or LexisNexis (things like and/or operators, synonyms, and parentheses). Any kind of advanced search features would be great; it doesn't have to be an exact match to any particular search engine.
动机:我有一个带有搜索框的HTML页面,后跟一堆段落(其中具有唯一ID并从JavaScript数组生成)。当用户在框中键入搜索查询并按Enter键时,所有段落都应隐藏(即他们的显示
设置为无
)如果它们与查询不匹配。
Motivation: I have an HTML page with a search box followed by a bunch of paragraphs (which have unique ids and are generated from a JavaScript array). When the user types a search query in the box and presses enter, all paragraphs should be hidden (i.e. their display
set to none
) if they don't match the query.
我当前的策略(使用jQuery):
My current strategy (using jQuery):
- 通过将查询字符串拆分为空格来将查询字符串分隔为关键字数组。
- 使用
$('p')隐藏所有段落。 hide()
。 - 对于每个关键字,使用
$('p:contains('+ + keyword +')显示包含它的段落)')。show()
。
- Separate the query string into an array of keywords by splitting it over whitespace.
- Hide all paragraphs with
$('p').hide()
. - For each keyword, show a paragraph containing it with
$('p:contains("'+keyword+'")').show()
.
这是一个非常有限的搜索功能,例如 - 敏感,将所有关键字视为可选,并且不提供和
,或
或括号等运算符。这也是低效的,因为即使它已经匹配,它也会为每个关键字遍历每个字符串一次。
Which is an extremely limited search feature that is case-sensitive, treats all keywords as optional, and doesn't provide operators like and
, or
, or parentheses. It's also inefficient because it goes through each string once for each keyword even if it has already been matched.
推荐答案
这里有一些库我正在评估项目(2013年7月)。其中任何一个都应该能够提供搜索功能的核心。
Here are some libraries that I am evaluating for projects (in July 2013). Any of these should be able to provide the core of the search feature.
- http://lunrjs.com/
- 词干,内置得分
- 13.8 kb minified
- 最近更新( https ://github.com/olivernn/lunr.js/commits/master )
- 10个贡献者
- 没有外部依赖 li>
- http://lunrjs.com/
- stemming, scoring built in
- 13.8 kb minified
- updated recently (https://github.com/olivernn/lunr.js/commits/master)
- 10 contributors
- no external dependencies
- 模糊搜索
- 1.58 kb缩小
- 最近更新( https://github.com/krisk/Fuse/commits/master )
- 1个投稿人
- 没有外部依赖
- fuzzy search
- 1.58 kb minified
- updated recently (https://github.com/krisk/Fuse/commits/master)
- 1 contributor
- no external dependencies
- 使用优质降级的html5存储空间
- 459 kb缩小
- 上次更新2013( https://github.com/reyesr/fullproof/commits/master )
- 2个贡献者
- 没有外部依赖
- uses html5 storage with graceful degradation
- 459 kb minified
- last updated 2013 (https://github.com/reyesr/fullproof/commits/master)
- 2 contributors
- no external dependencies
- 分页,内置模板
- 5.70 kb缩小
- 上次更新2014(
- 1个贡献者
- 取决于jquery和下划线
- pagination, templating built in
- 5.70 kb minified
- last updated 2014 (https://github.com/eikes/facetedsearch/commits/master)
- 1 contributor
- depends on jquery and underscore
如果你想建立你的拥有,这里是2个常见词干算法的实现,以帮助您入门:
If you feel like building your own, here are implementations of 2 common stemming algorithms to get you started:
- https://github.com/fortnightlabs/snowball-js
- http://tartarus.org/martin/PorterStemmer/
至于处理布尔逻辑搜索运算符,可能会关于js查询解析器的这个问题会很有用。
As for handling boolean logic search operators, maybe this question about js query parsers will be useful.
这篇关于用于搜索引擎样式搜索的JavaScript库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!