All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.takezoe.solr.scala.sample.SolrClientSample.scala Maven / Gradle / Ivy

There is a newer version: 0.0.27
Show newest version
package com.github.takezoe.solr.scala.sample

import com.github.takezoe.solr.scala.{Order, SolrClient, SolrClientFactory}
import com.github.takezoe.solr.scala.query.{ExpressionParser, GoogleExpressionParser}

object SolrClientSample extends App {
  case class Product(id: String, manu: Option[String], name: String)
  case class Param(name: String)

  implicit val parser: ExpressionParser = new GoogleExpressionParser()
//  implicit val solr = SolrClientFactory.dummy { request =>
//    println(request.getMethod)
//    println(request.getPath)
//    println(request.getParams)
//  }
  val client = new SolrClient("http://localhost:8983/solr")

  // register
//  client
//    .add(Map("id"->"001", "manu" -> null, "name" -> "ThinkPad X201s"))
//    .add(Map("id"->"002", "manu" -> "Lenovo", "name" -> "ThinkPad X202"))
//    .add(Map("id"->"003", "manu" -> "Lenovo", "name" -> "ThinkPad X100e"))
//    .commit()

  client
    .add(Product("001", None,           "ThinkPad X201s"))
    .add(Product("002", Some("Lenovo"), "ThinkPad X202"))
    .add(Product("003", Some("Lenovo"), "ThinkPad X100e"))
    .commit()

  // query (Map) without facet search
  val result1 = client.query("name:?name?")
        .fields("id", "manu", "name")
        .sortBy("id", Order.asc)
        .getResultAsMap(Map("name" -> "ThinkPad -X202"))

  println("-- matched documents --")
  result1.documents.foreach { doc =>
    println("id: " + doc("id"))
    println("  manu: " + doc.get("manu").getOrElse(""))
    println("  name: " + doc("name"))
  }
    
  // query (Map) with facet search
  val result2 = client.query("name:%name%")
        .fields("id", "manu", "name")
        .facetFields("manu")
        .sortBy("id", Order.asc)
        .getResultAsMap(Map("name" -> "ThinkPad -X201s"))

  println("-- matched documents --")
  result2.documents.foreach { doc =>
    println("id: " + doc("id"))
    println("  manu: " + doc.get("manu").getOrElse(""))
    println("  name: " + doc("name"))
  }

  println("-- facet counts --")
  result2.facetFields.foreach { case (field, counts) =>
    println("field: " + field)
    counts.foreach { case (manu, count) =>
      println("  " + manu + ": " + count)
    }
  }

  // query (case class)
  println("-- case class --")
  val result3 = client.query("name:%name%")
        .fields("id", "manu", "name")
        .facetFields("manu")
        .sortBy("id", Order.asc)
        .getResultAs[Product](Param("ThinkPad"))

  result3.documents.foreach { product =>
    println(product)
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy