0

I'm beginner in ElasticSearch. I'm trying to test if a list of geopoint (lat / long ) is existing in a list of geopoints.

For example I give this geopoint :

"lat": 49.01536940596998
"lon": 2.4967825412750244

and I want to test if this point exist in the list below. Thanks.

"positions": [
    {
      "millis": 12959023,
      "lat": 49.01525113731623,
      "lon": 2.4971945118159056,
      "rawX": -3754,
      "rawY": 605,
      "rawVx": 0,
      "rawVy": 0,
      "speed": 9.801029291617944,
      "accel": 0.09442740907572084,
      "grounded": true
    },
    {
      "millis": 12959914,
      "lat": 49.01536940596998,
      "lon": 2.4967825412750244,
      "rawX": -3784,
      "rawY": 619,
      "rawVx": -15,
      "rawVy": 7,
      "speed": 10.841861737855924,
      "accel": -0.09534648619563282,
      "grounded": true
    }
...
}
Community
  • 1
  • 1
Taybou
  • 3
  • 1
  • 2

1 Answers1

1

To be able to search in an array of objects, you need to use the nested data type. As the linked page explains, to keep the internal elements of the array as independent, you cannot use the default mapping. First, you will have to update the mapping.

Note: Mappings only take effect on new indexes. Reference.

PUT YOUR_INDEX
{
  "mappings": {
    "YOUR_TYPE": {
      "properties": {
        "positions": {
          "type": "nested" 
        }
      }
    }
  }
}

Now we can query the data. You're looking for a bool query, which combines other queries (in your case, term queries).

POST _search
{
  "query": {
    "nested": {
      "path": "positions",
      "query": {
        "bool" : {
          "must" : [
            { "term" : { "lat": 49.01536940596998  } },
            { "term" : { "lon": 2.4967825412750244 } }
          ]
        }
      }
    }
  }
}
fylie
  • 1,675
  • 1
  • 10
  • 14
  • 2
    Since you're modifying the mapping, why not introducing a `geo_point` field for the lat/lon values at the same time? – Val Nov 21 '16 at 05:01
  • @fylie thanks it works for us, actually we are working on a big documents with a lot of data (positions) and when we try to add this documents directly from sense extension It blocks, so what's the best solution to add this big data to elasticsearch and also how to optimize the request time because it will take time to get result . – Taybou Nov 27 '16 at 22:41
  • @Val , I tried to introduce a `geo_point` field for the lat/lon values but I'm getting error telling me that I could not do that because positions is composed of many other fields – Taybou Dec 02 '16 at 12:39
  • You need to create a `geo_point` field inside the `positions` one and not make `positions` a `geo_point` field – Val Dec 02 '16 at 12:40
  • @Val yes I understood that, but my problem is that I get this files or documents like that, and it represents a huge number of documents and i'm asking if there is a way to create a `geo_point` field inside each position with `elasticsearch` or other solutions. Do you have any ideas ? – Taybou Dec 04 '16 at 13:47