Search code examples
elasticsearchsearchelastic-appsearch

Implement search based on tags with elastic app-search


Context

I am building a website for a local commune, I have a list of "contact people" which among other things are assigned responsibilities.

Users of the site should be able to search for a given responsibility and then find the person that is assigned that area of work.

Problem

In contrast to native elastic-search the app-search platform only provides some very basic datatypes. I have decided to store the responsibilities as a list of comma separated values.

Example for a "contact person" document

{
    "firstname": "John",
    "lastname": "Doe",
    "responsibilities": "Fire safety, Budgeting, Weddings"
}

When I implement the search bar the typeahead will show the whole field

Which is obviously not a good experience. Another problem is that I cannot meaningfully facet and filter the values.

What have I tried

One possible approach is to create multiple "responsibility" fields like this:

{
    "responsibility_1": "Fire safety",
    "responsibility_2": "Budgeting",
    ...
}

But that feels rather dirty, requires me to implement a hard limit and still does not solve the faceting problem.

Something else I have thought of is disabling the responsibilities field for search. Then I would apply boosts and filters based on the current query. However, this would mean that can only search for a "responsibility" and not based on the other fields on the document.

How can I implement this kind of "tagging"? or am I out of luck when it comes to this kind of task and need native elastic?

I need some sort of "array" field type, but app-search does not implement such a type. I am tied to the app-search platform and migrating to native elastic-search would mean be a pretty big time commitment.


Solution

  • You need to create an array of values like this :

    {
        "firstname": "John",
        "lastname": "Doe",
        "responsibilities": ["Fire safety", "Budgeting", "Weddings"]
    }
    

    Arrays are supported in appsearch as mentioned in the documentation

    3. Arrays are supported