溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

solr4.7分組查詢facet怎么使用

發(fā)布時間:2021-12-22 17:37:21 來源:億速云 閱讀:169 作者:iii 欄目:互聯(lián)網(wǎng)科技

本篇內(nèi)容主要講解“solr4.7分組查詢facet怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“solr4.7分組查詢facet怎么使用”吧!

solr將以導(dǎo)航為目的的查詢結(jié)果稱為facet. 它并不會修改查詢結(jié)果信息, 只是在查詢結(jié)果上根據(jù)分類添加了count信息, 然后用戶根據(jù)count信息做進(jìn)一步的查詢, 比如淘寶的查詢列表中, 上面會表示不同的類目相關(guān)查詢結(jié)果的數(shù)量. 

比如搜索數(shù)碼相機(jī), 在搜索結(jié)果欄會根據(jù)廠商, 分辨率等維度列出, 這里廠商, 分辨率就是一個個facet. 

然后在廠商下面會有nikon, canon, sony等品牌, 這個叫約束(constraints) 

接下來是根據(jù)選擇, 列出當(dāng)前的導(dǎo)航路徑, 這個叫面包屑(breadcrumb). 

solr有幾種facet: 
普通facet, 比如從廠商品牌的維度建立fact 
查詢facet, 比如根據(jù)價格查詢時, 將根據(jù)價格, 設(shè)置多個區(qū)間, 比如0-10, 10-20, 20-30等 
日期facet, 也是一種特殊的范圍查詢, 比如按照月份進(jìn)行facet. 

facet的主要好處就是可以任意對搜索條件進(jìn)行組合, 避免無效搜索, 改善搜索體驗(yàn). 

facet都是在查詢時通過參數(shù)指定. 比如 
在http api中這樣寫: 

引用

"&facet=true&facet.field=manu"

java代碼 solrj這樣寫: 

   query.setFacet(true);//是否分組查詢
   query.setRows(0);//設(shè)置返回結(jié)果條數(shù),如果你時分組查詢,你就設(shè)置為0
   query.addFacetField("region");//增加分組字段   q
   query.addFacetField("theme");//增加分組字段   q
   QueryResponse rsp = server.query(query);
   //取出結(jié)果  
   FacetField facetField = rsp.getFacetField("region");
   List<Count> counts = null;
   if (facetField != null) {
     counts = facetField.getValues();
     if (counts != null) {
      for (Count count : counts) {
       if (count.getCount() != 0) {
        listRegion.add(count.getName()+"("+count.getCount()+")");
      }
      }
      map.put("region", listRegion);
     }
   }
   
   FacetField facetFieldTheme = rsp.getFacetField("theme");
   List<Count> countsTheme = null;
   if (facetFieldTheme != null) {
    countsTheme = facetFieldTheme.getValues();
     if (countsTheme != null) {
      for (Count count : countsTheme) {
         if (count.getCount() != 0) {
        listTheme.add(count.getName()+"("+count.getCount()+")");
      }
      }
      map.put("theme", listTheme);
     }
   }

而xml返回的結(jié)果為這樣: 

<lst name="facet_fields">
            <lst name="manu">
               <int name="Canon USA">17</int>
               <int name="Olympus">12</int>
               <int name="Sony">12</int>
               <int name="Panasonic">9</int>
               <int name="Nikon">4</int>
            </lst>
</lst>

通過java代碼可以這樣獲取facet結(jié)果:

List<FacetField> facetFields = queryResponse.getFacetFields();

在已有的查詢基礎(chǔ)上增加facet query, 可以這樣寫: 

solrQuery.addFacetQuery("quality:[* TO 10]")

比如對價格按照指定的區(qū)間進(jìn)行facet, 可以這樣加上facet后綴: 

引用

&facet=true&facet.query=price:[* TO 100] 
&facet.query=price:[100 TO 200];&facet.query=[price:200 TO 300] 
&facet.query=price:[300 TO 400];&facet.query=[price:400 TO 500] 
&facet.query=price:[500 TO *]



如果要對價格在400到500期間的產(chǎn)品做進(jìn)一步的搜索, 那么可以這樣寫(使用了solr的過濾查詢): 

引用

http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu&facet.field=camera_type &fq=price:[400 to 500]



注意這里的facet field不再包含price了 

如果這里對類型做進(jìn)一步的查詢, 那么query語句可以這樣寫: 

引用

http://localhost:8983/solr/select?q=camera &facet=on&facet.field=manu &fq=price:[400 to 500] &fq=camera_type:SLR 


facet的使用場景: 
1.類目導(dǎo)航 
2.自動提示, 需要借助一個支持多值的tag field. 
3.熱門關(guān)鍵詞排行, 也需要借助一個tag field 

到此,相信大家對“solr4.7分組查詢facet怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI