Indexing
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
```tut:silent import com.sksamuel.elastic4s.ElasticDsl._
Lets index a very simple document that has a single field, a name.
```tut:silent
indexInto("family" / "soprano") fields {
"head" -> "tony"
}
Very SQL like as you can see. We can also specify the id.
```tut:silent indexInto("family" / "soprano") fields { "boss" -> "tony" } id 1234
The id can be any object, it will be converted to a string using toString().
Multiple fields? Easy.
```tut:silent
indexInto("family" / "soprano") fields (
"boss" -> "tony",
"consigliere" -> "silvio",
"underboss" -> "bobby"
) id 1234
If we have a nested structure, we can specifiy nested fields using nested Map
s:
```tut:silent indexInto("family" / "soprano") fields ( "boss" -> Map( "name" -> "tony", "age" -> "56" ) )
Similarly arrays can be specified using `Array`s or `Seq`s:
```tut:silent
indexInto("family" / "soprano") fields (
"boss" -> "tony",
"members" -> Array(
"tony",
"salvidor",
"bobby"
),
"crews" -> Seq(
"gualtieri",
"baccalieri",
"barese",
"moltisanti"
)
)
More examples can be found in IndexDslTest.scala.
Indexing with Explicit Fields
Sometimes it is necessary to be able to explicitly specify fields, this can be done like:
```tut:silent import com.sksamuel.elastic4s._
indexInto("family" / "soprano") 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
Custom field types can be defined by extending `FieldValue`:
```tut:silent
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:
tut:silent
indexInto("twitter/tweets") fieldValues (
SimpleFieldValue("user", "tony.soprano"),
CustomDateFieldValue("post_date", new Date()),
SimpleFieldValue("message", "Spending time with the family")
)