简体   繁体   English

改善“搜索”输入字段的结果?

[英]Improve results of a “search” input field?

I have a database with 20,000 records. 我有一个包含20,000条记录的数据库。 Each record has a name. 每条记录都有一个名字。 When a user wants to view a record, he can visit a webapp and type the name of the record in an inputfield. 当用户想要查看记录时,他可以访问webapp并在输入字段中键入记录的名称。 While typing, results from the database would be shown/filtered matchin what the user typed. 在键入时,数据库的结果将显示/过滤用户键入的内容。 I would like to know the basic architecture/concepts on how to program this 我想知道如何编程的基本架构/概念

I'm using the following language stack: frontend: html5/javascript (+ajax to make instant calls while user is typing) backend: java + jdbc to connect to simple sql database 我正在使用以下语言堆栈:前端:html5 / javascript(+ ajax在用户输入时进行即时调用)后端:java + jdbc连接到简单的sql数据库

My initial idea is: 我最初的想法是:

  1. A user types text 用户键入文本
  2. Whenever a character is entered or removed in the inputfield, make an ajax request to the backend 每当在输入字段中输入或删除字符时,向后端发出ajax请求
  3. The backend does a LIKE %input% query on the name field in the database 后端对数据库中的name字段执行LIKE%input%查询
  4. All data found by the query is send as a json string to the frontend 查询找到的所有数据都作为json字符串发送到前端
  5. The frontend processes the json string and displays whatever results it finds 前端处理json字符串并显示它找到的任何结果

My two concerns are: the high amount of ajax requests to process, in conjunction with the possibly very heavy LIKE queries. 我的两个问题是:需要处理大量的ajax请求,以及可能非常繁重的LIKE查询。 What are ways to optimize this? 有什么方法可以优化这个? Only search for every two characters they type/remove? 只搜索他们输入/删除的每两个字符? Only query for the first ten results? 只查询前十个结果?

Do you know of websites that utilise these optimizations? 您知道利用这些优化的网站吗?

NOTE: assume the records are persons and names are like real people names, so some names are more common than others. 注意:假设记录是人,名称就像真人名,所以有些名称比其他名称更常见。

您可以选择SPA方法 - 将所有20 000个名称/ ID加载到客户端,然后在内存中过滤 - 它应该是最快的方式,对数据库和后端的负载最小

Here are possible solutions: 以下是可能的解决方案

  • Restirct search to prefix search - LIKE 'prefix%' can be executed efficiently using BTREE-type index. Restirct搜索前缀搜索 - LIKE'前缀%'可以使用BTREE类型索引有效执行。
  • Measure performance of naive LIKE '%str%' solution - it you are working on B2B application, database will likely load that table in memory and do queries fast enough. 测量天真的LIKE'%str%'解决方案的性能 - 你正在研究B2B应用程序,数据库可能会在内存中加载该表并快速进行查询。
  • Look at documentation for your database - there could be special features for that like inverted index 查看数据库的文档 - 可能有像倒置索引这样的特殊功能
  • as @Stepan Novikov suggested, load your data in memory and search manually 正如@Stepan Novikov建议的那样,将数据加载到内存中并手动搜索
  • Use specialized search indexers like SOLR or ElasticSearch (likely overkill for only 20k records) 使用像SOLR或ElasticSearch这样的专业搜索索引器(可能只有20k记录的过度杀伤)
  • If you are feeling ninja, implement your own N-gram index. 如果您感觉忍者,请实施您自己的N-gram指数。

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

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