Home > OS >  AND & OR condition in elasticsearch API
AND & OR condition in elasticsearch API

Time:09-28

I have address with address component stored in my elasticSearch, each address looks like following in my ES :

 {
                 "_index": "properties",
                 "_type": "_doc",
                 "_id": "property_5235354",
                 "_score": 32.839436,
                 "_source": {
                     "id": 5235354,
                     "branchid": 1,
                     "suburb": "Lyons",
                     "postcode": "2606",
                     "state": "ACT",
                     "@timestamp": "2021-09-27T08:56:08.827Z",
                     "agencycode": "X",
                     "address": "54-5 Burnie St Lyons ACT 2606 AUS",
                     "streetnumber": "5",
                     "branchcode": "X_ACT",
                     "unitnumber": "54",
                     "agencyid": 1,
                     "streetname": "Burnie St",
                     "@version": "1"
                 }
}

To search specific address on the basis of components I am considering following points :

  1. There could be abbreviation of street names like "James Street" -> "James St"
  2. Matching by address components with exact match in case insensitive manner
  3. Please let me know if you think I should consider something else

To do this I tried following :

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "streetname.keyword": "Burnie Street"
                    }
                },
                {
                    "match": {
                        "streetname.keyword": "Burnie St"
                    }
                }
            ],
            "must": [
                {
                    "match": {
                        "unitnumber.keyword": "54"
                    }
                },
                {
                    "match": {
                        "streetnumber.keyword": "5"
                    }
                },
                {
                    "match": {
                        "suburb.keyword": "Lyons"
                    }
                },
                {
                    "match": {
                        "state": "ACT"
                    }
                },
                {
                    "match": {
                        "postcode.keyword": "2606"
                    }
                }
            ]
        }
    },
    "size": 1000
}

Need your help on solving these :

  1. Above query is also returning invalid result like address : 54-5 Burnie Avenue Lyons ACT 2606 AUS which is Burnie Avenue not Burnie Street.
  2. If I give burnie street instead of Burnie Street, its unable to find the data.

More information : This is the full result of _search API with above request body where addresses 54-5 Burnie St Lyons ACT 2606 AUS & 54/5 Burnie Street Lyons ACT 2606 are right match but 54-5 Burnie Avenue Lyons ACT 2606 AUS is an invalid match

{
    "took": 1476,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 32.839436,
        "hits": [
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_5235354",
                "_score": 32.839436,
                "_source": {
                    "id": 5235354,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.827Z",
                    "agencycode": "X",
                    "address": "54-5 Burnie St Lyons ACT 2606 AUS",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie St",
                    "@version": "1"
                }
            },
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_11081",
                "_score": 28.954222,
                "_source": {
                    "id": 11081,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.163Z",
                    "agencycode": "X",
                    "address": "54/5 Burnie Street Lyons ACT 2606",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie Street",
                    "@version": "1"
                }
            },
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_5235356",
                "_score": 22.677355,
                "_source": {
                    "id": 5235356,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.847Z",
                    "agencycode": "X",
                    "address": "54-5 Burnie Avenue Lyons ACT 2606 AUS",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie Avenue",
                    "@version": "1"
                }
            }
        ]
    }
}

CodePudding user response:

You need to use a combination of bool/must/should query clause, term query (for exact match ignoring the case), and match_phrase_prefix query

Index Mapping:

{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "@version": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "agencycode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "agencyid": {
        "type": "long"
      },
      "branchcode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "branchid": {
        "type": "long"
      },
      "id": {
        "type": "long"
      },
      "postcode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "streetname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "streetnumber": {
        "type": "integer"
      },
      "suburb": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "unitnumber": {
        "type": "integer"
      }
    }
  }
}

Search Query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "streetnumber": "5"
          }
        },
        {
          "term": {
            "unitnumber": "54"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "streetname.keyword": {
                    "value": "Burnie Street",
                    "case_insensitive": "true"
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "streetname": "Burnie St"
                }
              }
            ]
          }
        },
        {
          "term": {
            "suburb.keyword": {
              "value": "Lyons",
              "case_insensitive": "true"
            }
          }
        },
        {
          "term": {
            "postcode.keyword": "2606"
          }
        },
        {
          "term": {
            "state.keyword": {
              "value": "ACT",
              "case_insensitive": "true"
            }
          }
        }
      ]
    }
  }
}
  • Related