如何在网站中实现搜索功能?
我想为网站实现搜索功能(假设它类似于 SO).我不想使用 Google 搜索此类内容.
I want to implement search functionality for a website (assume it is similar to SO). I don't want to use Google search of stuff like that.
我的问题是:
我该如何实施?
我知道有两种方法:
- 在用户提出查询时搜索应用程序中的所有数据库.
- 索引我拥有的所有数据并将其存储在其他地方并从那里查询(就像 Google 所做的那样).
谁能告诉我该走哪条路?有什么优点和缺点?
Can anyone tell me which way to go? What are the pros and cons?
更好,有没有更好的方法来做到这一点?
Better, are there any better ways to do this?
使用 lucene,
http://lucene.apache.org/java/docs/
Apache Lucene 是一个高性能、全功能的文本搜索引擎库,完全用 Java 编写.这是一项适用于几乎所有需要全文搜索的应用程序的技术,尤其是跨平台的应用程序.
Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.
它在 java 和 .net 中可用.它也可以在 php 中以 Zend 框架模块的形式使用.
It is available in java and .net. It is also in available in php in the form of a zend framework module.
Lucene 做你想做的事(搜索项目的索引),你必须跟踪一个 lucene 索引,但在性能方面它比做数据库搜索要好得多.顺便说一句,SO 搜索由 lucene 提供支持.:D
Lucene does what you wanted(indexing of the searched items), you have to keep track of a lucene index but it is much better than doing a database search in terms of performance. BTW, SO search is powered by lucene. :D