Doug Hughes
Doug Hughes

Reputation: 981

Can I query two different indexes with different structures and merge results in elasticsearch?

I'm trying to figure out if a particular type of query is possible with Elasticsearch and, if so, how to actually write that query.

I have two indexes, groups and challenges. The shape of the data for these two indexes are different, but they both have a field named id.

I want to search across different fields for the two indexes. For example, for groups I want to search across code, name, description. For challenges, it's the same fields, but also a few others, some in nested data.

I was able to get a simple query running that seemed to return the combination of results from both indexes using a URI query without specifying an index. EG: /search?q=some%20search. Cool.

However, I need to filter the results so that they only include groups and challenges that the current user is permitted to see. In my application I can easily get a list of these, permitted_group_ids and permitted_challenge_ids. I had planned on switching to a json body query and adding a filter, but I've not been able to figure out the syntax, especially regarding the filter.

So, a couple questions:

  1. What is the json query syntax to do a free-form query similar to /search?q=some%20search? The documentation seems to say that I should somehow use bool, but it seems like I need to manually specify the fields to search across (which are different between the two indexes) and that they all must match the search string.
  2. How can I filter the results to exclude based on id in different indexes?

In pseudocode, this is what I'm trying to accomplish:

Dear Elasticsearch,

Please find all records in the groups and challenges indexes that match the string "zamboni". Once you've identified those, please restrict the groups to only those whose id value is in [1, 2, 3] and the challenges to those whose id value is in [3, 4, 5]. Thanks!

Sincerely, Your Friendly Neighborhood Developer.

Any sort of help would be appreciated, even pointing me to specific bits of the Elasticsearch docs to read. Thank you!

Upvotes: 0

Views: 1225

Answers (1)

senorsunday
senorsunday

Reputation: 21

Something like this:

GET groups,challenges/_search
{
  "size": 10,
  "_source": false, // or ["whatever", "data", "you", "need"]
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {"bool": {"must": [
          {"term": {"_index": "groups"}},
          {"term": {"code": "zamboni"}},
          {"term": {"name": "zamboni"}},
          {"term": {"description": "zamboni"}},
          {"terms": {"id": [1, 2, 3]}}
        ]}},
        {"bool": {"must": [
          {"term": {"_index": "challenges"}},
          {"term": {"code": "zamboni"}},
          {"term": {"name": "zamboni"}},
          {"term": {"description": "zamboni"}},
          {"terms": {"id": [3, 4, 5]}}
        ]}}
      ]
    }
  }
} 

Upvotes: 1

Related Questions