Search code examples
elasticsearchelastic4s

elastic4s: Querying a multivalued field


I have a field which is multi valued. So how do we run queries for such a field?

Suppose 'Bank' document has a field 'city' which represents cities bank is present.

If I have to fetch all banks in either of a city in a group, what sort of query would help?

I tried matchQuery but it gives error:

def searchByCities(cities: Seq[String]) = {
    client.execute {
      search("bank").matchQuery("cities", cities) 
    }.await
  }

Error:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":1,"col":43}],"type":"parsing_exception","reason":"[match] unknown token [START_ARRAY] after [query]","line":1,"col":43},"status":400}

Solution

  • Assuming your cities are indexed as keywords, you can use terms query.

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

    I haven't use elastic4s myself, but if I understand correctly, the syntax should be like this:

    search("bank").query(termsQuery("cities", cities))