Taybou
Taybou

Reputation: 3

Looking for someone to help me with ElasticSearch

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
    }
...
}

Upvotes: 0

Views: 106

Answers (1)

fylie
fylie

Reputation: 1715

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 } }
          ]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions