我是一个完全的初学者,所以如果这个问题的措辞可能不正确或者我在某些事情上使用了错误的术语,请原谅。
我有一个 MS Word 文档,其中有四百条日记条目。我想编写的软件使用户能够在一个条目中搜索特定术语。所以我可能想要所有包含“快乐”和“悲伤”一词的日记条目。我想包括的另一个选项是搜索在例如之间写的所有日记条目。 2008年、2012年等
我希望搜索是“动态的”,即用户可以输入一个单词,并且在输入该单词时,结果表已经过滤了所有日记条目。因此,当用户输入单词时,表格会根据当前在搜索框中输入的内容而发生变化。
这可能吗?这个功能到底叫什么?您会推荐我哪种编程语言?我想把所有这些都放在网上,所以也许 php 或 ruby 会有用?
最佳答案
您选择的 Ruby-on-Rails 适合解决此问题:
- Store each diary entry as a database entry
- Search these entries using "full text search" - either on the db, or third party
- Return the "live" functionality with JS
MVC
Rails 使用 MVC
programming pattern使您能够保存到数据库中。这很重要,因为如果您要在 Rails 中进行开发,则需要遵循 MVC 模式:
基本上,您将日记条目保存在数据库
中,使用 Controller
来操作数据并使用 View
来显示数据:
#app/models/entry.rb
Class Entry < ActiveRecord::Base
#-> stores & brings back the entry data
end
#app/controllers/entries_controller.rb
Class EntriesController < ApplicationController
respond_to :js, :html, :json, only: :search
def index
@entries = Entry.all
end
def search
@entries = Entry.search params[:query]
respond_with @entries
end
end
#config/routes.rb
resources :entries do
collection do
match :search, via [:get, :post]
end
end
--
全文搜索
当您将请求发送到 Controller 时,处理搜索的方式将是 class method在您的 Entry 模型中(您从中获取数据的地方),或者引用 -
#app/models/entry.rb
Class Entry < ActiveRecord::Base
def self.search(query)
where("title LIKE '%#{query}%'")
end
end
您可以使用full text search for the SQL您正在使用的变体,或使用第三方搜索,例如 Solr或者什么东西
--
“实时”搜索
您可以使用 Javascript 进行“实时”搜索(例如 here ):
有一个number of tutorials关于如何在线执行此操作 - 只需 Google live searchrails
或 autocompleterails
。但是,所有实现的原理基本相同:
- JS will capture the text entered into a search box
- JS sends Ajax request to your Rails controller
- Controller sends response to ajax
- JS takes response & shows on screen
注意到这主要集中在 JS 元素上吗?这是我们使用的代码:
#app/assets/javascripts/application.js
//Livesearch
$(document).ready( function() {
var base_url = window.location.protocol + "//" + window.location.host;
$('#SearchSearch').searchbox({
url: base_url + '/search/',
param: 'search',
dom_id: '#livesearch',
loading_css: '#livesearch_loading'
})
});
#app/assets/javascripts/livesearch.js
// Author: Ryan Heath
// http://rpheath.com
(function($) {
$.searchbox = {}
$.extend(true, $.searchbox, {
settings: {
url: 'search',
param: 'search',
dom_id: '#livesearch',
minChars: 2,
loading_css: '#livesearch_loading',
del_id: '#livesearch_del'
},
loading: function() {
$($.searchbox.settings.loading_css).show()
},
idle: function() {
$($.searchbox.settings.loading_css).hide()
},
start: function() {
$.searchbox.loading()
$(document).trigger('before.searchbox')
},
stop: function() {
$.searchbox.idle()
$(document).trigger('after.searchbox')
},
kill: function() {
$($.searchbox.settings.dom_id).fadeOut(50)
$($.searchbox.settings.dom_id).html('')
$($.searchbox.settings.del_id).fadeOut(100)
},
reset: function() {
$($.searchbox.settings.dom_id).html('')
$($.searchbox.settings.dom_id).fadeOut(50)
$('#SearchSearch').val('')
$($.searchbox.settings.del_id).fadeOut(100)
},
process: function(terms) {
if(/\S/.test(terms)) {
$.ajax({
type: 'GET',
url: $.searchbox.settings.url,
data: {search: terms.trim()},
complete: function(data) {
$($.searchbox.settings.del_id).fadeIn(50)
$($.searchbox.settings.dom_id).html(data.responseText)
if (!$($.searchbox.settings.dom_id).is(':empty')) {
$($.searchbox.settings.dom_id).fadeIn(100)
}
$.searchbox.stop();
}
});
return false;
}else{
$.searchbox.kill();
}
}
});
$.fn.searchbox = function(config) {
var settings = $.extend(true, $.searchbox.settings, config || {})
$(document).trigger('init.searchbox')
$.searchbox.idle()
return this.each(function() {
var $input = $(this)
$input
.keyup(function() {
if ($input.val() != this.previousValue) {
if(/\S/.test($input.val().trim()) && $input.val().trim().length > $.searchbox.settings.minChars){
$.searchbox.start()
$.searchbox.process($input.val())
}else{
$.searchbox.kill()
}
this.previousValue = $input.val()
}
})
})
}
})(jQuery);
关于ruby-on-rails - 'dynamic'搜索的编程语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709222/