layout | title | section |
---|---|---|
docs |
Index API |
docs |
Indexing means adding a document to the search engine making it available for searching. All documents have an id and that can be specified by you or generated by Elasticsearch.
First, import the ElasticDSL
import com.sksamuel.elastic4s.ElasticDsl._
Lets index a very simple document that has a single field, a name.
indexInto("family") fields {
"head" -> "tony"
}
Very SQL like as you can see. We can also specify the id.
indexInto("family") fields {
"boss" -> "tony"
} id 1234
The id can be any object, it will be converted to a string using toString(). Multiple fields? Easy.
indexInto("family") fields (
"boss" -> "tony",
"consigliere" -> "silvio",
"underboss" -> "bobby"
) id 1234
If we have a nested structure, we can specifiy nested fields using nested Map
s:
indexInto("family") fields (
"boss" -> Map(
"name" -> "tony",
"age" -> "56"
)
)
Similarly arrays can be specified using Array
s or Seq
s:
indexInto("family") fields (
"boss" -> "tony",
"members" -> Array(
"tony",
"salvidor",
"bobby"
),
"crews" -> Seq(
"gualtieri",
"baccalieri",
"barese",
"moltisanti"
)
)
More examples can be found in IndexDslTest.scala.
Sometimes it is necessary to be able to explicitly specify fields, this can be done like:
import com.sksamuel.elastic4s._
indexInto("family") fieldValues (
SimpleFieldValue("boss", "tony"),
ArrayFieldValue("members", Array(
SimpleFieldValue("tony"),
SimpleFieldValue("salvidor"),
SimpleFieldValue("bobby")
)),
ArrayFieldValue("crews", Seq(
SimpleFieldValue("gualtieri"),
SimpleFieldValue("baccalieri"),
SimpleFieldValue("barese"),
SimpleFieldValue("moltisanti")
))
)
Custom field types can be defined by extending FieldValue
:
import java.util.Date
import java.text.SimpleDateFormat
import org.elasticsearch.common.xcontent.XContentBuilder
case class CustomDateFieldValue(name: String, date: Date) extends FieldValue {
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
def output(source: XContentBuilder): Unit = {
source.field(name, dateFormat.format(date))
}
}
This can then be used when indexing:
indexInto("tweets") fieldValues (
SimpleFieldValue("user", "tony.soprano"),
CustomDateFieldValue("post_date", new Date()),
SimpleFieldValue("message", "Spending time with the family")
)