2010-08-12 141 views
3
用草堆了woosh搜索引擎

喜IM:草垛多個領域搜索

我的模型看起來如下

class Person(models.Model): 
    personid = models.IntegerField(primary_key = True, db_column = 'PID') 
    firstname = models.CharField(max_length = 50, db_column = 'FIRSTNAME') 
    lastname = models.CharField(max_length = 50, db_column = 'LASTNAME') 
    class Meta: 
     db_table = '"TEST"."PERSON"' 
     managed = False 


class TDoc(models.Model): 
    tdocid = models.IntegerField(primary_key = True, db_column = 'TDOCID') 
    person = models.ForeignKey(Person, db_column = 'PID') 
    content = models.TextField(db_column = 'CONTENT', blank = True) 
    filepath = models.TextField(db_column = 'FILEPATH', blank = True) 
    class Meta: 
     db_table = '"TEST"."TDOC"' 
     managed = False 

的search_index.py如下:

class TDocIndex(SearchIndex): 

    content = CharField(model_attr = 'content', document = True) 
    filepaht = CharField(model_attr = 'filepath') 
    person = CharField(model_attr = 'person') 

    def get_queryset(self): 
     return TDoc.objects.all() 

    def prepare_person(self, obj): 
     # Store a list of id's for filtering 
     return obj.person.lastname 

site.register(TDoc, TDocIndex) 

我問題是我想做多個提交搜索,如

內容:xxx和人物:史密斯

乾草堆上一次搜索他們,我不能做特定的字段搜索。 我懷疑我的索引已損壞,但:

ix = open_dir("/testindex") 

searcher = ix.searcher() 

mparser = MultifieldParser(["content", "filepath", "person"], schema = ix.schema) 
myquery = mparser.parse(content:xxx AND person:SMITH') 
results = searcher.search(myquery) 
for result in results: 
    print result 

,但它的工作原理和回報是正確的值。 我米使用標準的草垛搜索查看,search.html從教程

(r'^search/', include('haystack.urls')), 

回答

4

在你的索引,你應該定義與document=True,這是文件草垛將搜索的一個領域。按照慣例,該字段被命名爲text。如果您打算對其值進行過濾或排序,則可以添加額外的字段。

當您執行搜索時,需要考慮多個字段的方法是將文檔定義爲模板,並在文檔字段上設置use_template。索引會是什麼樣子:

class TDocIndex(SearchIndex): 

    text = CharField(document=True, use_template=True) 

    #if you plan to filter by person 
    personid = IntegerField(model_attr='person__id') 

site.register(TDoc, TDocIndex) 

而且你必須像一個搜索/索引/ tdoc_text.txt模板:

{{ object.content }} 
{{ object.filepath }} 
{{ object.person.lastname }} 

this答案。