Search code examples
elasticsearchelasticsearch-5elastic4s

creating elastic search query


I have this json doc in my elasticsearch:

{
  "personId": "5b564b6a0c000b622a55",
  "name": "Jake Harper",
  "country": "US",
  "socialSecurityNumber": 7634904,
  "personAddress": {
    "city": "Los Angeles",
    "street": "Sunset BLVD",
    "streetNumber": 149,
  },
  "additionalAddresses": [
    {
      "addressType": "office",
      "additionalAddress": {
        "city": "Santa Monica",
        "street": "3rd street",
        "streetNumber": 13
      }
    },
    {
      "addressType": "property",
      "additionalAddress": {
        "city": "mxkwUcc branch city",
        "street": "mxkwUcc BLVD",
        "streetNumber": 255
      }
    }
  ]
}

and I want to create an elastic query that will help me to find people by:

personId
socialSecurityNumber
personAddress(all fields)
additionalAddresses(all fields in th array docs)

and im having trouble with creating the query specially with personAddress and additionalAddresses... can anyone give me some kind of direction here..? thanks!

currently my query looks like :

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "personId": "5b564b6a0c000b622a"
          }
        },
        {
          "match": {
            "name": "Harper"
          }
        }
      ]
    }
  }
}

im using multiple query cause I will get a term input and I want to check if its part of any of the above fields.

my mappings:

{
  "peopledb": {
    "mappings": {
      "person": {
        "properties": {
          "additionalAddresses": {
            "properties": {
              "additionalAddress": {
                "properties": {
                  "city": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "street": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "streetNumber": {
                    "type": "long"
                  },
                  "zipCode": {
                    "type": "long"
                  }
                }
              },
              "addressType": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "country": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "personAddress": {
            "properties": {
              "city": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "street": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetNumber": {
                "type": "long"
              },
              "zipCode": {
                "type": "long"
              }
            }
          },
          "personId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Solution

  • You might need to make additionalAddresses of type nested, but first let's see if multi_match gets you a bit further:

    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "personId": "5b564b6a0c000b622a"
              }
            },
            {
              "match": {
                "name": "Harper"
              }
            },
            {
              "match": {
                "personAddress.city": "Los"
              }
            },
            {
              "multi_match": {
                "fields": ["additionalAddresses.additionalAddress.city", "additionalAddresses.additionalAddress.street", "additionalAddresses.additionalAddress.streetNumber"],
                "query": "123 Main Street"
              }
            }
          ]
        }
      }
    }