本文介绍了Sunspot Solr搜索,例如Rails活动记录"LIKE"搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的应用程序中使用正常的轨活动记录(如LIKE)搜索,所以我开始使用黑子solr搜索.我希望它尽可能地接近"LIKE"搜索.

Hi I have been using the normal rails active record LIKE search in my app, I started using sunspot solr search. I would like it to act as close to the rails LIKE search as possible.


wine.rb

#sunspot stuff
  searchable :auto_index => true, :auto_remove => true do
  text :name
end
#sunspot stuff


solr/conf/schema.xml

<fieldType name="text" class="solr.TextField" omitNorms="false">
  <analyzer>
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StandardFilterFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>


application_controller.rb

search_string = "will input some values here"
query = "%" + search_string + "%"

solr_search = Wine.search do
  fulltext search_string
end
@solr_search_results = solr_search.results.sort_by{|e| e[:id]}

@rails_search_results = Wine.find(:all, :conditions => ['wines.name LIKE ?' , query]).sort_by{|e| e[:id]}


search1
search_string =盛大"


search1
search_string = "grand"

@solr_search_results
186大自然保护区
688 Grand Plaisir Cabernet Sauvignon Shiraz Malbec Petit Verdot Cabernet Franc
760 Grand Vin Blanc
768大胭脂
857尊贵特级佳酿
1067 Grand Classique
1584年Grand Vin De Glenelly
3389大康斯坦斯马斯喀特
3708 GrandCuvèeBrut
3857大康斯坦斯马斯喀特

@rails_search_results
186大自然保护区
688 Grand Plaisir Cabernet Sauvignon Shiraz Malbec Petit Verdot Cabernet Franc
760 Grand Vin Blanc
768大胭脂
857尊贵特级佳酿
969 River Grandeur Cape Blend
972 River Grandeur Cabernet Sauvignon
973河霞多丽霞多丽
974 River Chen勃朗峰Chenin Blanc
975 River Grandeur Pinotage
976河长相思干白葡萄酒
977设拉子格兰德河
978玫瑰大河
1067 Grand Classique
1584年Grand Vin De Glenelly
3389大康斯坦斯马斯喀特
3708 GrandCuvèeBrut
3857大康斯坦斯马斯喀特

似乎Solr找不到与"Grandeur"相对应的条目,而只是找到了完全匹配的条目?我该如何解决?

@solr_search_results
186 Grand Reserve
688 Grand Plaisir Cabernet Sauvignon Shiraz Malbec Petit Verdot Cabernet Franc
760 Grand Vin Blanc
768 Grand Rouge
857 Premier Grand Cru
1067 Grand Classique
1584 Grand Vin De Glenelly
3389 Grand Constance Muscat
3708 Grand Cuvèe Brut
3857 Grand Constance Muscat

@rails_search_results
186 Grand Reserve
688 Grand Plaisir Cabernet Sauvignon Shiraz Malbec Petit Verdot Cabernet Franc
760 Grand Vin Blanc
768 Grand Rouge
857 Premier Grand Cru
969 River Grandeur Cape Blend
972 River Grandeur Cabernet Sauvignon
973 River Grandeur Chardonnay
974 River Grandeur Chenin Blanc
975 River Grandeur Pinotage
976 River Grandeur Sauvignon Blanc
977 River Grandeur Shiraz
978 River Grandeur Rose
1067 Grand Classique
1584 Grand Vin De Glenelly
3389 Grand Constance Muscat
3708 Grand Cuvèe Brut
3857 Grand Constance Muscat

Seems like Solr did not find the entries with 'Grandeur' in them, it only found the exact matches? How can I fix this?


search2
search_string ="rood"


search2
search_string = "rood"

@solr_search_results
200 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3198 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3394 Rood

@rails_search_results
200 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
483 Roodeberg Red Cabernet Sauvignon Shiraz Merlot
484罗德堡白长相思霞多丽陈宁维奥涅尔
1113 Zevenrood
3044 Roodewal
3198 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3394 Rood
3477 Roodeberg Red
3478 Roodeberg White
3594 Roodeberg White
3604 Roodeberg Red

同样的事情在这里发生,当search_string ='rood'时,solr找不到'Roodeberg',它只找到了完全匹配的

@solr_search_results
200 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3198 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3394 Rood

@rails_search_results
200 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
483 Roodeberg Red Cabernet Sauvignon Shiraz Merlot
484 Roodeberg White Sauvignon Blanc Chardonnay Chenin Viognier
1113 Zevenrood
3044 Roodewal
3198 Dassies Rood Cinsaut Cabernet Sauvignon Ruby Cabernet
3394 Rood
3477 Roodeberg Red
3478 Roodeberg White
3594 Roodeberg White
3604 Roodeberg Red

Same thing happens here, solr did not find the 'Roodeberg' when search_string = 'rood', it only found the exact match

感谢DanS
,将nGram过滤器添加到schema.xml中进行部分匹配.但仍未显示所有结果

Added nGram filter to schema.xml for partial matching, Thanks to DanS
But still not showing all results

app/solr/conf/schema.xml

<fieldType name="text" class="solr.TextField" omitNorms="false">

    <analyzer type="index">
        <tokenizer class="solr.LowerCaseTokenizerFactory"/>
        <filter class="solr.NGramFilterFactory" minGramSize="3" maxGramSize="15"/>
    </analyzer>

    <analyzer type="query">
        <tokenizer class="solr.LowerCaseTokenizerFactory"/>
    </analyzer>

</fieldType>



它仍然不能完全满足我的要求,请看以下示例

search3
search_string ="merl"



It still does not quite do what I want it to do, have a look at the following example

search3
search_string = "merl"

@solr_search_results
130梅鹿lot
202梅鹿lot
306梅鹿lot
336梅鹿lot
556梅鹿lot
579梅鹿lot
592梅鹿lot
623梅鹿lot
640梅鹿lot
689梅鹿lot
694梅洛
714梅鹿lot
776梅鹿lot
790梅鹿lot
841梅洛
865梅鹿lot
891梅鹿lot
947梅洛
1015梅鹿lot
1045梅鹿lot
1046梅鹿lot
1073梅洛
1075梅洛
1089梅洛
1096梅鹿lot
1111梅洛
1121梅洛
1144梅鹿lot
1145梅鹿lot
1169梅洛

@rails_search_results
34骑士储备混合梅洛赤霞珠法郎赤霞珠西拉子
129 Matt Black Cabernet Sauvignon Shiraz Merlot Petit Verdot Mourvedre Pinotage
130梅鹿lot
202梅鹿lot
240格拉帕赤霞珠梅洛红葡萄酒
306梅鹿lot
336梅鹿lot
416干玫瑰酒梅洛
477 Orchestra Cabernet Sauvignon Malbec Merlot Cab Franc Sh​​iraz
483 Roodeberg Red Cabernet Sauvignon Shiraz Merlot
556梅鹿lot
579梅鹿lot
592梅鹿lot
614赤霞珠梅洛
623梅鹿lot
640梅鹿br
656梅洛赤霞珠法郎长相思书法
672 Ondine Merlot
689梅鹿lot
694梅洛
696酒桶选择梅乐(Merlot)
714梅鹿lot
762梅洛私人珍藏
776梅鹿lot
790梅鹿lot
795梅洛红葡萄酒私人收藏
816 JJ手工葡萄酒梅鹿<
832金三角梅鹿lot
841梅洛
842梅洛·罗斯(MerlotRosé)
854鹰冠赤霞珠梅洛红葡萄酒
865梅鹿lot
877位酿酒师选择美乐设拉子
891梅鹿lot
892梅洛保护区
893 Mountain Red Shiraz Merlot
941酒窖精选梅洛赤霞珠
943葡萄园选择Canetnet长相思梅洛红葡萄酒Cab Franc Sh​​iraz
947梅洛
982 Boet Erasmus Cabernet Sauvignon Merlot Malbec Petit Verdot
983 Cara Cabernet Sauvignon Shiraz Merlot
984经典赤霞珠西拉子梅洛
1010 Laureat Cabernet Sauvignon Merlot
1015梅鹿lot
1045梅鹿lot
1046梅鹿lot
1073梅洛
1075梅洛
1079赤霞珠/梅洛
1089梅洛
1093 Adelberg赤霞珠梅洛红葡萄酒
1096梅鹿lot
1104 Z Collection Cabernet Franc Merlot Cabernet Sauvignon
1111梅鹿lot
1121梅洛
1144梅鹿lot
1145梅鹿lot
1169梅洛
1186梅鹿lot
1254赤霞珠梅洛红葡萄酒
1260赤霞珠/梅洛
1261梅洛
1269梅洛
1326梅洛
1349赤霞珠梅洛红葡萄酒
1364品种选择梅鹿<
1381梅洛
1384赤霞珠梅洛红葡萄酒
1393赤霞珠梅洛红葡萄酒
1401赤霞珠梅洛红葡萄酒
1404梅鹿lot
1421小赤霞珠梅洛红葡萄酒
1424梅洛
1431系列梅洛
1443梅鹿lot
1454梅鹿lot
1467年扑克山西拉子梅洛葡萄酒
1468梅鹿lot
1476梅鹿lot
1491年情况梅洛
1495孔雀岭梅尔洛
1542梅洛
1543梅洛
1549梅鹿lot
1552梅洛保护区
1582未过滤的梅鹿<
1592梅洛
3001梅洛
3007赤霞珠梅洛红葡萄酒
3036赤霞珠梅洛红葡萄酒
3056梅鹿<
3067犹太洁食梅鹿br
3073有机梅鹿<
3079 Premium Merlot
3091梅洛
3106梅洛(Melot)和少量的马尔贝克(
)3133赤霞珠梅洛红葡萄酒
3143梅洛五种气候
3154 1号梅洛后备队
3182拉诺伊赤霞珠梅洛红葡萄酒
3183 Reserve Collection Cab Sauv Merlot Cab Franc
3200美乐(Merlot)
3236 Giorgio Cabernet Sauvignon梅洛小珀特·维多德设拉子
3258丹妮·德·湿赤霞珠梅洛红葡萄酒
3276红色赤霞珠梅洛红色赤霞珠法郎小西点设拉子
3288梅鹿<
3303四重奏Pinotage赤霞珠长相思梅洛设拉子
3307多样性梅洛·马尔贝克
3311 Vineyard Creations梅鹿<
3318 Caapmans赤霞珠梅洛红葡萄酒
3321 Luipaardsberg Merlot
3322梅洛
3326 Rhinofields梅鹿lot
3334梅鹿lot
3343梅鹿lot
3363梅洛赤霞珠干红葡萄酒
3372梅鹿lot
3390梅鹿lot
3416 R 62梅洛赤霞珠
3418拔下62 MerlotRosé
3419拔下62 Merlot Shiraz
3431梅洛
3439 KC赤霞珠梅洛红葡萄酒
3471 Orchestra Cabernet Sauvignon Malbec Merlot Cab Franc Sh​​iraz
3497梅鹿lot
3498梅洛赤霞珠红葡萄酒
3510梅鹿<
3531梅洛
3540梅鹿lot
3560梅鹿lot
3568梅洛玫瑰
3578特别版梅洛
3581梅鹿<
3584赤霞珠梅洛红葡萄酒
3624梅鹿<
梅洛3642酒窖精选
3657梅鹿lot
3677梅鹿lot
3681梅洛
3685系列C赤霞珠梅洛红葡萄酒法郎
3693梅鹿lot
3728亚历山大·方丹·梅洛
3755孔雀岭梅尔洛
3771梅洛老博物馆
3773地窖精选赤霞珠梅洛红葡萄酒
3820梅鹿<
3859梅洛
3882邓斯顿·梅洛(Dunstone Merlot)
3900 Duckitt梅洛赤霞珠红葡萄酒
3919梅洛
3947梅洛

@solr_search_results
130 Merlot
202 Merlot
306 Merlot
336 Merlot
556 Merlot
579 Merlot
592 Merlot
623 Merlot
640 Merlot
689 Merlot
694 Merlot
714 Merlot
776 Merlot
790 Merlot
841 Merlot
865 Merlot
891 Merlot
947 Merlot
1015 Merlot
1045 Merlot
1046 Merlot
1073 Merlot
1075 Merlot
1089 Merlot
1096 Merlot
1111 Merlot
1121 Merlot
1144 Merlot
1145 Merlot
1169 Merlot

@rails_search_results
34 Cavalier Reserve Blend Merlot Cabernet Franc Cabernet Sauvignon Shiraz
129 Matt Black Cabernet Sauvignon Shiraz Merlot Petit Verdot Mourvedre Pinotage
130 Merlot
202 Merlot
240 Grappa Cabernet Merlot Premium
306 Merlot
336 Merlot
416 Dry Rosè Merlot
477 Orchestra Cabernet Sauvignon Malbec Merlot Cab Franc Shiraz
483 Roodeberg Red Cabernet Sauvignon Shiraz Merlot
556 Merlot
579 Merlot
592 Merlot
614 Cabernet Merlot
623 Merlot
640 Merlot
656 Calligraphy Merlot Cabernet Franc Sauvignon Blanc
672 Ondine Merlot
689 Merlot
694 Merlot
696 Barrel Select Merlot
714 Merlot
762 Private Collection Merlot
776 Merlot
790 Merlot
795 Private Collection Merlot
816 JJ Handmade Wines Merlot
832 Golden Triangle Merlot
841 Merlot
842 Merlot Rosé
854 Eagle Crest Cabernet Sauvignon Merlot
865 Merlot
877 Winemakers Choice Merlot Shiraz
891 Merlot
892 Merlot Reserve
893 Mountain Red Shiraz Merlot
941 Cellar Selection Merlot Cabernet Sauvignon
943 Vineyard Selection Cabertnet Sauvignon Merlot Cab Franc Shiraz
947 Merlot
982 Boet Erasmus Cabernet Sauvignon Merlot Malbec Petit Verdot
983 Cara Cabernet Sauvignon Shiraz Merlot
984 Classic Cabernet Sauvignon Shiraz Merlot
1010 Laureat Cabernet Sauvignon Merlot
1015 Merlot
1045 Merlot
1046 Merlot
1073 Merlot
1075 Merlot
1079 Cabernet/Merlot
1089 Merlot
1093 Adelberg Cabernet Sauvignon Merlot
1096 Merlot
1104 Z Collection Cabernet Franc Merlot Cabernet Sauvignon
1111 Merlot
1121 Merlot
1144 Merlot
1145 Merlot
1169 Merlot
1186 Merlot
1254 Cabernet Sauvignon Merlot
1260 Cabernet Sauvignon/Merlot
1261 Merlot
1269 Merlot
1326 Merlot
1349 Cabernet Sauvignon Merlot
1364 Cultivar Selection Merlot
1381 Merlot
1384 Cabernet Sauvignon Merlot
1393 Cabernet Sauvignon Merlot
1401 Cabernet Sauvignon Merlot
1404 Merlot
1421 Petit Cabernet Sauvignon Merlot
1424 Merlot
1431 Collection Merlot
1443 Merlot
1454 Merlot
1467 Poker Hill Shiraz Merlot
1468 Merlot
1476 Merlot
1491 Circumstance Merlot
1495 Peacock Ridge Merlot
1542 Merlot
1543 Merlot
1549 Merlot
1552 Merlot Reserve
1582 Unfiltered Merlot
1592 Merlot
3001 Merlot
3007 Cabernet Sauvignon Merlot
3036 Cabernet Sauvignon Merlot
3056 Merlot
3067 Kosher Merlot
3073 Organic Merlot
3079 Premium Merlot
3091 Merlot
3106 Merlot with a dash of Malbec
3133 Cabernet Sauvignon Merlot
3143 Five Climates Merlot
3154 Reserve No1 Merlot
3182 Lanoy Cabernet Sauvignon Merlot
3183 Reserve Collection Cab Sauv Merlot Cab Franc
3200 Merlot
3236 Giorgio Cabernet Sauvignon Merlot Petit Verdot Shiraz
3258 Danie De Wet Cabernet Sauvignon Merlot
3276 Red Cabernet Sauvignon Merlot Cab Franc Petit Verdot Shiraz
3288 Merlot
3303 Quartet Pinotage Cabernet Sauvignon Merlot Shiraz
3307 Diversity Merlot Malbec
3311 Vineyard Creations Merlot
3318 Caapmans Cabernet Sauvignon Merlot
3321 Luipaardsberg Merlot
3322 Merlot
3326 Rhinofields Merlot
3334 Merlot
3343 Merlot
3363 Merlot Cabernet Sauvignon
3372 Merlot
3390 Merlot
3416 R 62 Merlot Cabernet Sauvignon
3418 Unplugged 62 Merlot Rosé
3419 Unplugged 62 Merlot Shiraz
3431 Merlot
3439 KC Cabernet Sauvignon Merlot
3471 Orchestra Cabernet Sauvignon Malbec Merlot Cab Franc Shiraz
3497 Merlot
3498 Merlot Cabernet Sauvignon
3510 Merlot
3531 Merlot
3540 Merlot
3560 Merlot
3568 Merlot Rose
3578 Special Edition Merlot
3581 Merlot
3584 Cabernet Sauvignon Merlot
3624 Merlot
3642 Cellar Selecti on Merlot
3657 Merlot
3677 Merlot
3681 Merlot
3685 Series C Cabernet Sauvignon Merlot Cab Franc
3693 Merlot
3728 Alexanderfontein Merlot
3755 Peacock Ridge Merlot
3771 The Old Museum Merlot
3773 Cellar Selection Cabernet Sauvignon Merlot
3820 Merlot
3859 Merlot
3882 Dunstone Merlot
3900 Duckitt Merlot Cabernet Sauvignon
3919 Merlot
3947 Merlot

推荐答案

Update2

已修复!似乎Solr默认情况下仅显示30个条目,因此它仅显示最相关的匹配项,并且跳过了我也想要的其他匹配项.
我添加了此文件

Update2

Fixed! Seems Solr shows only 30 entries by default, so it showed only the most relevant matches and skipped the other I also wanted.
I added this file

myapp/config/initializers/sunspot_solr.rb

myapp/config/initializers/sunspot_solr.rb

Sunspot.config.pagination.default_per_page = 3000

这篇关于Sunspot Solr搜索,例如Rails活动记录"LIKE"搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:50