Search code examples
c#elasticsearchnest

Searching across multiple address fields in Elasticsearch


I have an address saved in an elasticsearch document in the form of:

       "address": {
          "streetAddress": "123 Fake street",
          "city": "San Francisco",
          "region": "California",
          "postalCode": "94111",
          "country": "United States"
       },

How can I search the index using an input string that may contain data for each of the different fields. For example the user may search for "123 Fake street, San Francisco" - with or without the comma.

Would it make sense to tokenize the search string and then search for each word against each of the fields in questions? Or somehow concatenate all of the 5 fields into a separate address field and search on that? Currently we're using ngrams of size 2 - 12.

I had a question I asked here about phone numbers where I concatenated all of the phone number elements into one new field: Searching phone number object in Elasticsearch with C#

But the address situation is somewhat different in how uses may search for the documents.


Solution

  • If you plan on sticking with Elastic 5 for a while, the _all field should satisfy your requirements.

    As @Paige Cook pointed out in the comments above, the _all field will be disabled by default in Elastic 6, with no way of enabling it on newly created indices in that version. It will, however, stay enabled on indices created in version 5.

    If you read the section about the _all field in the breaking changes documentation listed above, you'll see references to the query_string and simple_query_string methods (Both available through the NEST library as well as part of the HTTP API elastic provides) both of which will continue searching across all fields, even though the _all field is now disabled.

    Hopefully one of these can solve the issue you are facing.