Idkt
Idkt

Reputation: 2996

Using elastic4s for GeoDistance queries

I'm having some trouble understanding the DSL syntax for forming GeoDistance queries (get all entries within a certain distance from a point);

Currently, in my ES index "places" -> "shops" with id = 1 I have a sample entry;

{
  _index: "places",
  _type: "shops",
  _id: "1",
  _version: 5,
  found: true,
  _source: {
    location: {
      lat: 50,
      lon: 50
    },
    name: "coffee store",
    posted: "2014-09-12T08:53:01.673Z"
  }
}

And then, using the Elastic4s DSL, I'm building up a search query;

val nearbyStoresFuture:Future[SearchResponse] = client execute {
  search in "places" -> "shops" filter {
    geoDistance("location") point(lat, lon) distance(distance)
  }
}
nearbyStoresFuture onComplete {
  case Success(s) => {
    s
    client.close
  }
  case Failure(t) => {
    t
    client.close
  }
}

and in this case:

lat: Double = 50.0
lon: Double = 50.0
distance: String = "50km"

So in any case, if the query is correct, this should give me results containing the entry in ES since it is at the same point as the entry, and therefore within a distance of 50km.

The query is successful but the results are empty.

EDIT: Here's how the mapping looks like;

ES.execute {
  create index "places" mappings(
    "shops" as (
      "location" typed GeoPointType,
      "name" typed StringType,
      "posted" typed DateType
      )
    )
}

What's wrong with this?

Upvotes: 0

Views: 695

Answers (1)

sksamuel
sksamuel

Reputation: 16387

This is not an elastic4s question, but a general elasticsearch question: tl;dr You are not indexing any fields.

When you index data, you specify which fields are indexed, and those fields are then used for searching. In addition, you can store a document which can be fetched when a search matches that document. This is useful if you want to be able to return the original document, but want to use a subset of fields or derived fields when indexing.

In elastic4s

index into "myindex"->"mytype" fields ("name"->name, "location"->latlong) source srcdoc

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-source-field.html http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/index-doc.html

Upvotes: 1

Related Questions