设为首页 收藏本站
查看: 1034|回复: 0

[经验分享] Solr的Scala客户端(scalikesolr)介绍

[复制链接]

尚未签到

发表于 2015-7-16 12:43:16 | 显示全部楼层 |阅读模式
  本文是scalikesolr的wiki的翻译
后边的代码片段使用了如下文档产生的索引"example/exampledocs/books.json".
{
  "id" : "978-0641723445",
  "cat" : ["book","hardcover"],
  "title" : "The Lightning Thief",
  "author" : "Rick Riordan",
  "series_t" : "Percy Jackson and the Olympians",
  "sequence_i" : 1,
  "genre_s" : "fantasy",
  "inStock" : true,
  "price" : 12.50,
  "pages_i" : 384
},
{
  "id" : "978-1423103349",
  "cat" : ["book","paperback"],
  "title" : "The Sea of Monsters",
  "author" : "Rick Riordan",
  "series_t" : "Percy Jackson and the Olympians",
  "sequence_i" : 2,
  "genre_s" : "fantasy",
  "inStock" : true,
  "price" : 6.49,
  "pages_i" : 304
}
查询
简单查询
使用核心查询参数和普通查询参数:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • import com.github.seratch.scalikesolr._  
  •   
  • val client = Solr.httpServer(new URL("http://localhost:8983/solr")).newClient  
  • val request = new QueryRequest(writerType = WriterType.JavaBinary, query = Query("author:Rick")) // faster when using WriterType.JavaBinary  
  • val response = client.doQuery(request)  
  • println(response.responseHeader)  
  • println(response.response)  
  • response.response.documents foreach {  
  •   case doc => {  
  •     println(doc.get("id").toString()) // "978-1423103349"  
  •     println(doc.get("cat").toListOrElse(Nil).toString) // List(book, hardcover)  
  •     println(doc.get("title").toString()) // "The Sea of Monsters"  
  •     println(doc.get("pages_i").toIntOrElse(0).toString) // 304  
  •     println(doc.get("price").toDoubleOrElse(0.0).toString) // 6.49  
  •   }  
  • }  
  
从SolrDocument绑定到对象
需要无参构造器和字段设置器。也可以指定有一个字符串作为参数的构造器的用户定义类型




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • case class PageI(val value: String = "")  
  • case class Book(  
  •   var id: String = "",  
  •   var cat: List[String] = Nil,  
  •   var price: Double = 0.0,  
  •   var pageI: PageI = PageI(),  
  •   var sequenceI: Int = 0 ) {  
  •   def this() = {  
  •     this ("", Nil, 0.0, PageI(), 0)  
  •   }  
  • }  
  • val book = doc.bind(classOf[Book])  
  • println(book.id) // "978-1423103349"  
  • println(book.cat.size) // 2  
  • println(book.price) // 6.49  
  • println(book.pageI.value) // 304  
  • println(book.sequenceI) // 2  
  
使用高亮
使用高亮参数:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new QueryRequest(  
  •   writerType = WriterType.JSON, // but JSON format might be slow...  
  •   query = Query("author:Rick"),  
  •   sort = Sort("page_i desc")  
  • )  
  • request.highlighting = HighlightingParams(true)  
  • val response = client.doQuery(request)  
  • println(response.highlightings)  
  • response.highlightings.keys foreach {  
  •   case key => {  
  •     println(key + " -> " + response.highlightings.get(key).get("author").toString)  
  •     // "978-0641723445" -> "Rick Riordan"  
  •   }  
  • }  
  
使用MoreLikeThis
使用推荐:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new QueryRequest(Query("author:Rick"))  
  • request.moreLikeThis = MoreLikeThisParams(  
  •   enabled = true,  
  •   count = 3,  
  •   fieldsToUseForSimilarity = FieldsToUseForSimilarity("body")  
  • )  
  • val response = client.doQuery(request)  
  • println(response.moreLikeThis)  
  • response.response.documents foreach {  
  •   doc => {  
  •     val id = doc.get("id").toString  
  •     response.moreLikeThis.getList(id) foreach {  
  •       case recommendation => {  
  •         println(recommendation) // "SolrDocument(WriterType(standard),,Map(start -> 0, numFound -> 0))"  
  •       }  
  •     }  
  •   }  
  • }  
  
使用多层面查询(FacetQuery)
使用简单的多层面查询参数:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new QueryRequest(Query("author:Rick"))  
  • request.facet = new FacetParams(  
  •   enabled = true,  
  •   params = List(new FacetParam(Param("facet.field"), Value("title")))  
  • )  
  • val response = client.doQuery(request)  
  • println(response.facet.facetFields)  
  • response.facet.facetFields.keys foreach {  
  •   case key => {  
  •     val facets = response.facet.facetFields.getOrElse(key, new SolrDocument())  
  •     facets.keys foreach {  
  •       case facetKey => println(facetKey + " -> " + facets.get(facetKey).toIntOrElse(0))  
  •       // "thief" -> 1, "sea" -> 1, "monster" -> 1, "lightn" -> 1  
  •     }  
  •   }  
  • }  
  
使用结果集分组(Groupiong) /字段折叠(Field Collapsing)




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new QueryRequest(Query("genre_s:fantasy"))  
  • request.group = new GroupParams(  
  •   enabled = true,  
  •   field = Field("author_t")  
  • )  
  • val response = client.doQuery(request)  
  • println(response.groups.toString)  
  • response.groups.groups foreach {  
  •   case group => println(group.groupValue + " -> " + group.documents.toString)  
  •   // "r.r" -> List(SolrDocument(...  
  •   // "glen" -> List(SolrDocument(...  
  • }  
  
分布式查询
使用分布式查询:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new QueryRequest(Query("genre_s:fantasy"))  
  • request.shards = new DistributedSearchParams(  
  •   shards = List(  
  •     "localhost:8984/solr",  
  •     "localhost:8985/solr"  
  •   )  
  • )  
  • val response = client.doQuery(request)  
  • println(response.groups.toString)  
  
数据导入命令(DIH Command)
数据导入的命令:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new DIHCommandRequest(command = "delta-import")  
  • val response = client.doDIHCommand(request)  
  • println(response.initArgs)  
  • println(response.command)  
  • println(response.status)  
  • println(response.importResponse)  
  • println(response.statusMessages)  
  

文档更新
更新Solr索引的XML信息:
添加/更新文档
向Solr中添加文档:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new UpdateRequest()  
  • val doc1 = SolrDocument(  
  •   writerType = WriterType.JSON,  
  •   rawBody = """  
  •   { "id" : "978-0641723445",  
  •     "cat" : ["book","hardcover"],  
  •     "title" : "The Lightning Thief",  
  •     "author" : "Rick Riordan",  
  •     "series_t" : "Percy Jackson and the Olympians",  
  •     "sequence_i" : 1,  
  •     "genre_s" : "fantasy",  
  •     "inStock" : true,  
  •     "price" : 12.50,  
  •     "pages_i" : 384  
  •   }"""  
  • )  
  • val doc2 = SolrDocument(  
  • writerType = WriterType.JSON,  
  • rawBody = """  
  •   { "id" : "978-1423103349",  
  •     "cat" : ["book","paperback"],  
  •     "title" : "The Sea of Monsters",  
  •     "author" : "Rick Riordan",  
  •     "series_t" : "Percy Jackson and the Olympians",  
  •     "sequence_i" : 2,  
  •     "genre_s" : "fantasy",  
  •     "inStock" : true,  
  •     "price" : 6.49,  
  •     "pages_i" : 304  
  •   }"""  
  • )  
  • request.documents = List(doc1, doc2)  
  • val response = client.doUpdateDocuments(request)  
  • client.doCommit(new UpdateRequest)  
  
删除文档




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new DeleteRequest(uniqueKeysToDelete = List("978-0641723445"))  
  • val response = client.doDeleteDocuments(request)  
  • client.doCommit(new UpdateRequest)  
  • Commit  
  •   
  • val response = client.doCommit(new UpdateRequest())  
  • Rollback  
  •   
  • val response = client.doRollback(new UpdateRequest())  
  • Optimize  
  •   
  • val response = client.doOptimize(new UpdateRequest())  
  • Add / Update documents in CSV format  
  •   
  • val request = new UpdateRequest(  
  •   requestBody = "id,name,sequence_i\n0553573403,A Game of Thrones,1\n..."  
  • )  
  • val response = client.doUpdateDocumentsInCSV(request)  
  • client.doCommit(new UpdateRequest)  
  
以XML格式更新:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new UpdateRequest(  
  •   requestBody = ""  
  • )  
  • val response = client.doUpdateInXML(request)  
  
以JSON格式更新:




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val request = new UpdateRequest(  
  •   writerType = WriterType.JSON,  
  •   requestBody = "{ 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;optimize7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;: { 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;waitFlush7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;:false, 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;waitSearcher7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;:false } }"  
  • )  
  • val response = client.doUpdateInJSON(request)  
  
从更新格式中加载文档
不支持JSON格式
XML格式




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val xmlString = "05991Bridgewater..."  
  • val docs = UpdateFormatLoader.fromXMLString(xmlString)  
  • docs foreach {  
  •   case doc => {  
  •     println("employeeId:" + doc.get("employeeId").toString()) // "05991"  
  •     println("office:" + doc.get("office").toString()) // "Bridgewater"  
  •   }  
  • }  
  
CSV格式




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

  • val csvString = "id,name,sequence_i\n0553573403,A Game of Thrones,1\n..."  
  • val docs = UpdateFormatLoader.fromCSVString(csvString)  
  • docs foreach {  
  •   case doc => {  
  •     println(doc.get("id")) // "0553573403"  
  •     println(doc.get("name")) // "A Game of Thrones"  
  •     println(doc.get("sequence_i").toIntOrElse(0)) // 1  
  •   }  
  • }  
  

Ping




Java代码 http://guanjinke.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf

    • val response = client.doPing(new PingRequest())  
    • println(response.status) // "OK"  


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87311-1-1.html 上篇帖子: Solr 环境搭建 下篇帖子: Solr的自动完成实现方式(第二部分:Suggester方式)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表