2015-05-18 63 views
2

我需要添加全局搜索,它將搜索幾個集合(例如客戶端,訂單,項目......)並將所有答案放在一個列表中。什麼是在流星中實現全局搜索(在多個集合中搜索)的最佳方式

我已經看過EasySearchmeteorhacks:search-source,但它們只支持當時在一個集合中進行搜索。

有人已經解決了這個問題嗎?什麼是最好的開始?

+0

沒有for循環不行?不知道你想要完成什麼,但是合併多個結果數組不應該太難 –

+0

你究竟想用數據做什麼?可能有很多不錯的選擇,但很難說清楚。在你想搜索的所有人中你有一些共同的鑰匙嗎?或者你正在尋找全文搜索? –

+0

我的主要觀點可能是問某人是否已經實施了一個很好的解決方案,以便像一個集合中的「EasySearch」包一樣在多個集合中進行搜索。也可能有人已經用我提到的軟件包做了很好的建議。 當然,我可以使用ElasticSearch或Mongos自己的搜索來實現我自己的搜索,但爲什麼如果有人已經做了自己的搜索就可以創建自己的搜索。 – Jaro

回答

4

經過幾天的研究終於找到了EasySearch這樣提供多個集合的搜索,它只是隱藏在文檔中。

所有需要的是:

EasySearch.createSearchIndex('collection1', { 
    'field' : ['name'], 
    'collection' : Collection1, 
    'use' : 'minimongo', 
    'limit' : 20, 
}); 

EasySearch.createSearchIndex('collection2', { 
    'field' : ['name', 'text'], 
    'collection' : Collection2, 
    'use' : 'minimongo', 
    'limit' : 20, 
}); 

Template.yourTemplate.indexes = ['players', 'cars']; 

HTML

<div class="search-input"> 
    <!-- indexes is a javascript array which holds 'players' and 'cars' --> 
    {{> esInput index=indexes placeholder="Search..." }} 
</div> 

<div class="results-wrapper"> 
    {{#esEach index="players"}} 
     {{> player}} 
    {{/esEach}} 

    {{#esEach index="cars"}} 
     {{> car}} 
    {{/esEach}} 
</div> 

此外,我建議在這個問題上閱讀討論:https://github.com/matteodem/meteor-easy-search/issues/10


Update after EasySearch changed their API. Now you should do:

Collection1Index = EasySearch.Index({ 
    'collection': Collection1, 
    'fields': ['name', 'description'], 
    'engine': new EasySearch.MongoDB() 
}); 

Collection2Index = EasySearch.Index({ 
    'collection': Collection2, 
    'fields': ['name', 'text'], 
    'engine': new EasySearch.MongoDB() 
}); 

Template.yourTemplate.helpers ({ 
    indexes: function() { return [Collection1Index, Collection2Index] } 
    attributes: function() { return {placeholder: "Search clients, orders and suppliers...", class: 'form-control'} } 
    Collection1Index: function() { return Collection1Index } 
    Collection2Index: function() { return Collection2Index } 
}); 

HTML

<div class="search-input"> 
    {{> EasySearch.Input indexes=indexes attributes=attributes }} 
</div> 

<div class="results-wrapper"> 
    {{#EasySearch.Each index=Collection1Index}} 
     {{> player}} 
    {{/EasySearch.Each}} 

    {{#EasySearch.Each index=Collection2Index}} 
     {{> car}} 
    {{/EasySearch.Each}} 
</div> 
+0

我無法搜索多個索引以進行工作,我認爲API已經發生了變化,例如現在是EasySearch.Input而不是esInput。我得到的只是一個錯誤「錯誤:未收到索引或索引數組:」[object Object],[object Object]「[invalid-configuration]」。我已閱讀有關該問題的討論,但無法弄清楚。使用「索引=索引」只是給了我一個不同的錯誤!任何人都可以使用新的API發佈一個工作示例嗎? –

+0

我剛剛添加了適用於我的最新EasySearch更改的更新示例。 – Jaro

+1

我認爲{{> EasySearch indexes = indexes attributes = attributes}}需要{{> EasySearch.Input indexes = indexes attributes = attributes}}? –

相關問題