0

I am very new to elasticsearch, and I am trying to write a query. I have this sample data:

{
  firstName:”x”, 
  lastName: “y”,
  latitude: “18”, 
  longitude: “19”,
  address: {
    addressLine1: “abcd”, 
    addressLine2: “wasd”,
    city: “dallas”, 
    state:”texas”, 
    country: null,  
  }
}, 
{
  firstName:”q”, 
  lastName: “a”,
  latitude: “20”, 
  longitude: “34”,
  address: {
    addressLine1: “fdffw”, 
    addressLine2: “weer”,
    city: “erg”, 
    state:”earth”, 
    country: rgrtg,     
  }
}

I am trying to write a query in elasticsearch that would produce the latitude and longitude given the fields in the address section. Any of these fields could be null, but based on the match of the remaining fields, the query should perform a match and return a result. So far, I have tried this, but it returns all of the data instead of specific json objects.

{
  "query": {
  "bool": {
    "must": [
    {
      "bool": {
        "should": [
          {
            "match": {
              "address.addressLine1": "abcd"
            }
          },
          {
            "match": {
              "address.addressLine2": "wasd"
            }
          }
        ]
      }
    }
  ],
  "filter": {
    "bool": {
      "should": [
        { "exists": { "field": "address.addressLine1" } },
        { "exists": { "field": "address.addressLine2" } },
        { "exists": { "field": "address.city" } },
        { "exists": { "field": "address.state" } },
            { "exists": { "field": "address.country" } }
          ]
        }
      }
    }
  },
  "_source": ["latitude", "longitude"]
}

The above query should return:

{
  firstName:”x”, 
  lastName: “y”,
  latitude: “18”, 
  longitude: “19”,
  address: {
    addressLine1: “abcd”, 
    addressLine2: “wasd”,
    city: “dallas”, 
    state:”texas”, 
    country: null,  
  }
}

Can someone please help me with this? I am stuck and not understanding what is wrong.

shasha0607
  • 123
  • 1
  • 9

0 Answers0