1

I'm currently trying to create a percolator query with Elastic4s. I've got about this far but I can't seem to find any examples so I'm not sure how this quite works. So I've got:

val percQuery = percolate in esIndex / esType query myQuery

esClient.execute(percQuery)

Every time it runs it doesn't match anything. I figured out I need to be able to percolate on an Id but I can't seem to find any examples on how to do it, not even in the docs. I know with Elastic4s creating queries other than a percolator query lets you specify an id field like:

val query = index into esIndex / esType source myDoc id 12345

I've tried this way for percolate but it doesn't like the id field, does anyone know how this can be done?

I was using Dispatch Http to do this previously but I'm trying to move away from it. Before, I was doing this to submit the percolator query:

url(s"$esUrl/.percolator/$queryId)
  .setContentType("application/json", "utf-8")
  .setBody(someJson)
  .POST

notice the queryId just need something similar to that but in elastic4s.

Ryan Wilson
  • 1,743
  • 3
  • 15
  • 26

2 Answers2

0

So you want to add a document and return the queries that are waiting for that id to be added? That seems an odd use for percolate as it will be a one time use only, as only one document can be added per id. You can't do a percolate currently on id in elastic4s, and I'm not sure if you can even do it in elasticsearch itself.

This is the best attempt I can come up with, where you have your own "id" field, which could mirror the 'proper' _id field.

object Test extends App {

  import ElasticDsl._
  val client = ElasticClient.local

  client.execute {
    create index "perc" mappings {
      "idtest" as(
        "id" typed StringType
        )
    }
  }.await

  client.execute {
    register id "a" into "perc" query {
      termQuery("id", "a")
    }
  }.await

  client.execute {
    register id "b" into "perc" query {
      termQuery("id", "b")
    }
  }.await

  val resp1 = client.execute {
    percolate in "perc/idtest" doc("id" -> "a")
  }.await

  // prints a
  println(resp1.getMatches.head.getId)

  val resp2 = client.execute {
    percolate in "perc/idtest" doc("id" -> "b")
  }.await

  // prints b
  println(resp2.getMatches.head.getId)
}

Written using elastic4s 1.7.4

sksamuel
  • 16,154
  • 8
  • 60
  • 108
  • Well I was using Dispatch Http to do this previously but I'm trying to move away from it. Before, I was doing this to submit the percolator query: `url(s"$esUrl/.percolator/$queryId)` notice the queryId just need something similar to that. Do you think this is still the best way? – Ryan Wilson Dec 22 '15 at 16:42
0

So after much more researching I figured out how this works with elastic4s. To do this in Elastic4s you actually have to use register instead of percolate like so:

val percQuery = register id queryId into esIndex query myQuery

This will register a percolator query at the id.

Ryan Wilson
  • 1,743
  • 3
  • 15
  • 26
  • Register is the act of registering your queries. Percolate is the act of adding a document and seeing which registered queries it matches. I think the terminology is consistent with Elasticsearch, – sksamuel Jan 05 '16 at 01:26