Skip to content

Latest commit

 

History

History
109 lines (87 loc) · 2.42 KB

index.md

File metadata and controls

109 lines (87 loc) · 2.42 KB

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.

Lets index a very simple document that has a single field, a name.

index into "family" -> "soprano" fields {
  "head" -> "tony"
}

Very SQL like as you can see. We can also specify the id.

index into "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.

index into "family" -> "soprano" fields (
  "boss" -> "tony",
  "consigliere" -> "silvio",
  "underboss" -> "bobby"
) id 1234

If we have a nested structure, we can specifiy nested fields using nested Maps:

index into "family" -> "soprano" fields (
  "boss" -> Map(
    "name" -> "tony",
    "age" -> "56"
  )
)

Similarly arrays can be specified using Arrays or Seqs:

index into "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:

index into "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:

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:

index into "twitter/tweets" fieldValues (
  SimpleFieldValue("user", "tony.soprano"),
  CustomDateFieldValue("post_date", new Date()),
  SimpleFieldValue("message", "Spending time with the family")
)