0

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?

Ashesh
  • 2,978
  • 4
  • 27
  • 47
  • Are you indexing the location? The contents of the _source field are not automatically indexable, they are just available for fetching. – sksamuel Sep 12 '14 at 10:32
  • The mappings that I put on ES just set the `location` field's type to `GeoPointType`; I've added an edit to the post to explain better. Does that enable indexing on the location field by default? @monkjack – Ashesh Sep 12 '14 at 10:40
  • Setting the mapping is correct. But when you actually "index" (insert) the data, it looks like you're not telling Elasticsearch what the location field is. Having it in the _source is not enough, you have to make it indexable. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-source-field.html – sksamuel Sep 12 '14 at 10:46
  • @monkjack thanks again for the super quick response. What I changed in the mapping is this; `"location" typed GeoPointType latLon(true),` which I believe makes this field indexable on lat, lon. I now get one hit, but without fields. Last question: is that expected behaviour? If yes, how do we get the fields? – Ashesh Sep 12 '14 at 11:01
  • That's what I am saying. You are not indexing the data (the fields). When you index you must specify which fields to index. Creating the mapping is only used as the schema for the index. http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/index-doc.html – sksamuel Sep 12 '14 at 11:14

1 Answers1

1

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

sksamuel
  • 16,154
  • 8
  • 60
  • 108
  • Pardon my confusion, but that is pretty much exactly what I'm doing; I'm `index`ing the document with `index into "places" -> "shops" fields ("location -> GeoPoint.parseFromLatLon(…)")` but that doesn't seem to yield any hits. I think I need to go through the ES documentation and understand how it all works. – Ashesh Sep 12 '14 at 11:42