首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ExtJs4的Solr live搜索

问使用ExtJs4的Solr live搜索
EN

Stack Overflow用户
提问于 2012-03-20 13:23:10
回答 2查看 983关注 0票数 0

我在后台使用了solr+haystack(django plugin),搜索运行良好;

虽然Django(和Haystack)和它的模板为我做了所有的事情(我的意思是它的配置和使用都很简单),但ExtJS4有点复杂;

问题是如何在ExtJS4中使用Solr?

一个例子是非常感谢的;

感谢您的帮助,并为我的英语表达歉意;

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-25 14:20:14

由于ExtJS4是一个MVC框架,所以解决方案就像MVC一样;

The controller/Search.js

代码语言:javascript
复制
Ext.define('yourapp.controller.Search',{
extend:'Ext.app.Controller',

stores:[
    'Searches'
],

views:[
    'search.Search',
    'search.SearchList'
],

models:[
    'Search'
],

init:function(){
    this.control({
        "search":{
            'keyup':this.search,                
        },
    });
},

search:function(inputedTxt, e, eOpts){
    this.getSearchesStore().load({
        //When sending a request, q will rely to request.POST['q'] on server-side;
        //inputedTxt.getValue() -- a value, entered in textfield (or whatever)
        params:{
            q:inputedTxt.getValue()
        },
        callback:function(result){
            if(result[0]){
                //do something with the result
                //i'd been creating a window with a grid inside. "Grid"'s view is written below.
            }
        }               
    }
});

The models/Search.js

代码语言:javascript
复制
Ext.define('yourapp.model.Search',{
    extend:'Ext.data.Model',

    fields:[
        {name:'name',   type:'string'}
    ]   
});

The store/Searches.js

代码语言:javascript
复制
Ext.define('yourapp.store.Searches',{
extend:'Ext.data.Store',
storeId: "searchStore",

model:'yourapp.model.Search',

autoLoad: false,

proxy:{
    type:'ajax',
    // server-side url
    url: '/searchlist/',

    actionMethods:{create: "POST", read: "POST", update: "POST", destroy: "POST"},
    reader:{
        type:'json',
        root:'searches'
    }
}
});

The view/search/Search.js

代码语言:javascript
复制
//a Text field to input text;
Ext.define('yourapp.view.search.Search',{
extend:'Ext.form.field.Text',
alias: 'widget.search',
id:    "searchView",

enableKeyEvents: true,
initComponent:function(){
    this.callParent(arguments);     
}
});

The view/search/SearchList.js

代码语言:javascript
复制
//a view for a result
Ext.define('yourapp.view.search.SearchList',{
extend: 'Ext.grid.Panel',

alias:'widget.searchlist',
title: 'search result',
store: 'Searches',

columns:[
        { 
        header:'Name', 
        dataIndex:'name',
        flex:1                      
    }
]
});

应在view/Viewport.js xtype: 'search',中的某个位置插入要显示的文本字段。

这就是ExtJS4部件的全部内容。

在服务器端-- Django:'haystack‘和Solr应该被安装和配置(’配置‘我的意思是:搜索应该已经在服务器端工作);

在someapp/view.py中

代码语言:javascript
复制
def searchlist(request):    
    from haystack.query import SearchQuerySet
    # POST["q"] should be receivedt from our client-side
    searchText = request.POST["q"]

    sqs = SearchQuerySet().filter(name=searchText)

    data = []
    for result in sqs:            
        data.append({"name": result.object.name})

return HttpResponse('{ success:true, searches:'+simplejson.dumps(data)+'}', mimetype = 'application/json')

最后,您应该在urls.py中添加:

(r'^searchlist/','someapp.views.searchlist'),

这就够了。谨致问候。

P.S.

我知道这不是最好的答案,也缺乏解释,但对我来说,我更喜欢代码示例而不是口头解释。

票数 1
EN

Stack Overflow用户

发布于 2012-03-21 10:29:26

SOLR使用wt=json参数从其查询中获得JSON输出,并且可以很容易地被ExtJS使用。

http://wiki.apache.org/solr/SolJSON?action=fullsearch&context=180&value=jsonp&titlesearch=Titles#JSON_Response_Writer

如果需要使用jsonp,可以通过此参数json.wrf=callback指定回调函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9781713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档