本文是scalikesolr的[url=https://github.com/seratch/scalikesolr/wiki/Overview-in-Scala]wiki[/url]的翻译
后边的代码片段使用了如下文档产生的索引"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
}
[b][size=large]查询[/size][/b]
简单查询
使用[url=http://wiki.apache.org/solr/CoreQueryParameters]核心查询参数[/url]和[url=http://wiki.apache.org/solr/CommonQueryParameters]普通查询参数[/url]:
[b]从SolrDocument绑定到对象[/b]
需要无参构造器和字段设置器。也可以指定有一个字符串作为参数的构造器的用户定义类型
[b]使用高亮[/b]
使用高亮参数:
[b]使用MoreLikeThis[/b]
使用推荐:
[b]使用多层面查询(FacetQuery)[/b]
使用简单的多层面查询参数:
使用结果集分组(Groupiong) /字段折叠(Field Collapsing)
[b]分布式查询[/b]
使用分布式查询:
[b]数据导入命令(DIH Command)[/b]
数据导入的命令:
[b]文档更新[/b]
[url=http://wiki.apache.org/solr/UpdateXmlMessages]更新Solr索引的XML信息[/url]:
添加/更新文档
向Solr中添加文档:
删除文档
以XML格式更新:
以JSON格式更新:
[b]从更新格式中加载文档[/b]
不支持JSON格式
XML格式
CSV格式
Ping
后边的代码片段使用了如下文档产生的索引"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
}
[b][size=large]查询[/size][/b]
简单查询
使用[url=http://wiki.apache.org/solr/CoreQueryParameters]核心查询参数[/url]和[url=http://wiki.apache.org/solr/CommonQueryParameters]普通查询参数[/url]:
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
}
}
[b]从SolrDocument绑定到对象[/b]
需要无参构造器和字段设置器。也可以指定有一个字符串作为参数的构造器的用户定义类型
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
[b]使用高亮[/b]
使用高亮参数:
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" -> "[i]Rick[/i] Riordan"
}
}
[b]使用MoreLikeThis[/b]
使用推荐:
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))"
}
}
}
}
[b]使用多层面查询(FacetQuery)[/b]
使用简单的多层面查询参数:
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)
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(...
}
[b]分布式查询[/b]
使用分布式查询:
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)
[b]数据导入命令(DIH Command)[/b]
数据导入的命令:
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)
[b]文档更新[/b]
[url=http://wiki.apache.org/solr/UpdateXmlMessages]更新Solr索引的XML信息[/url]:
添加/更新文档
向Solr中添加文档:
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)
删除文档
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格式更新:
val request = new UpdateRequest(
requestBody = "<optimize/>"
)
val response = client.doUpdateInXML(request)
以JSON格式更新:
val request = new UpdateRequest(
writerType = WriterType.JSON,
requestBody = "{ 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;optimize7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;: { 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;waitFlush7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;:false, 7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;waitSearcher7b1debea0390ffbb4e49e2f145a1625ea6ec7011quot;:false } }"
)
val response = client.doUpdateInJSON(request)
[b]从更新格式中加载文档[/b]
不支持JSON格式
XML格式
val xmlString = "<add><doc><field name=ece0aa4e60e0be0d48ee70d07ac10cec4e21eecaquot;employeeIdece0aa4e60e0be0d48ee70d07ac10cec4e21eecaquot;>05991</field><field name=ece0aa4e60e0be0d48ee70d07ac10cec4e21eecaquot;officeece0aa4e60e0be0d48ee70d07ac10cec4e21eecaquot;>Bridgewater</field>..."
val docs = UpdateFormatLoader.fromXMLString(xmlString)
docs foreach {
case doc => {
println("employeeId:" + doc.get("employeeId").toString()) // "05991"
println("office:" + doc.get("office").toString()) // "Bridgewater"
}
}
CSV格式
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
val response = client.doPing(new PingRequest())
println(response.status) // "OK"