在服务器中实现自动完成的最佳方式是什么?[英] What's the best way to implement AutoComplete in the server?

本文是小编为大家收集整理的关于在服务器中实现自动完成的最佳方式是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这个问题很容易.使自动完成美丽在客户端的网络应用程序是很简单.有很多插件.



但是,在后端,在服务器端,什么是最好的方法呢?我不喜欢这个想法,每次按下用户按下DB.



我一直在想sphinx或一些全文搜索引擎并行运行



例如,如果我有一个PHP(高流量)网站,我可以创建一个并行python脚本,从我的"自动填充文本框".然后,当用户在客户端按下一个键时,AJAX请求被定向到可以使用特殊策略的python脚本.



你的aproach是什么? / p>

一些惯例:




  • 尝试不要碰到DB.我的意思是,得到
    请求并做某事SELECT *
    FROM foo WHERE bar LIKE"req%"不是
    一个很好的答案.这可能是一个好的
    策略,但我知道如何做.*

  • 复制数据可能是一个不错的选择.


推荐答案

我同意你需要一些更好的解决方案. Apache solr 有一个"建议"功能,您可以很好地使用.如果你的数据集是小的,那么把所有的数据在内存中,只是做一个简单的循环.



在前端,我建议使用setTimeout()等待大约200毫秒,然后触发ajax调用.如果在那200ms,触发另一个键击,然后取消最后一个超时,并启动另一个.这是一个真正干净的解决方案,它不会击中db每次击键.我在过去使用它,它的工作原理很好.



这解释了jQuery的solr以及如何创建一个自动完成.
//www. mattweber.org/2009/05/02/solr-autosuggest-with-termscomponent-and-jquery/


本文地址:https://www.itbaoku.cn/post/597690.html

问题描述

This question is easy people. Make autocomplete beautiful in the client side of a web app is simple. There are a lot of plugins.

But, in the backside, in the server side, what's the best way to do it? I don't like the idea to hit the DB with each keypressed by the user.

I've been thinking about sphinx, or some full-text search engine running parallel from your site.

For example, if i have a PHP (high traffic) web site, i can create a parallel python script that get http requests from my "autocomplete textboxes". Then, when a user is pressing a key in the client side, the AJAX requests are directed to that python script that can use a special strategy.

What's your aproach?

Some conventions:

  • Try not to hit the DB. I mean, get the request and do something SELECT * FROM foo WHERE bar LIKE "req%" is not a good answer. It may be a good strategy, but i know how to do it.*
  • Replicated data can be a good choice.

推荐答案

I do agree that you need to have some better solution. Apache solr has a "suggestion" feature that you can use pretty well. If your data set is small then put all the data in memory and just do a simple loop.

On the front end, I recommend using setTimeout() to wait for about 200ms before firing the ajax call. If in that 200ms, another keystroke is triggered, then cancel the last timeout and start another one. This is a really clean solution where it wouldn't hit the db with each keystroke. I have used it in the past and it works really well.

This explains solr with jquery and how to create an autocomplete really well. http://www.mattweber.org/2009/05/02/solr-autosuggest-with-termscomponent-and-jquery/