Multi-index query returning no results

Hey guys, I have 2 different indexes that store information about my users.

Both use a hashed version of their id_number as their doc_id, I`m trying to create a query that will look for different fields in both indexes and return me the users that match. My current problem is that even tho I have users that match my query, my query keeps returning 0 results.

Index1 data

{
  "_index" : "index1",
  "_type" : "_doc",
  "_id" : "7820b6dfed99773990650168c55b1135",
  "_score" : 12.400421,
  "_source" : {
    "user_id" : 25,
    "date" : "2023-08-01T10:10:37",
    "name": "John",
    "age" : 20,
    "country" : "US",
    "sessions" : 8
  }
}

Index2 Data

{
  "_index" : "index2",
  "_type" : "_doc",
  "_id" : "7820b6dfed99773990650168c55b1135",
  "_score" : 12.400421,
  "_source" : {
    "user_id" : 25,
    "company_id": 1,
    "status": "active"
  }
}

Query

GET /index1,index2/_search
{
  "query": {
    "bool": {
      "must" : [
        {
          "match": {"status": "active"}
        },
        {
          "bool": {
            "should" : [
              {
                "match": {"age": 20}
              },
              {
                "range": {
                  "sessions": {"gte": 5}
                }
              }
            ]
          }
        }
      ],
      "must_not" : [
        {
          "match": {
            "company_id": 2
          }
        }
      ]
    }
  }
}

For some reason, the query works just fine if I only include fields from the same index in the must argument, but as soon as I add a field from a different index in the must statement it returns 0 results

"status" is in index 2 but not index 1; therefore, you are not getting match for any documents from index 1. You can use the same logic on your "bool" section.
multi index search means using that exact same searching payload on multiple indices.
Ideally, you want the same mapping on the searchable fields.

Would it be possible to create a query that would return all doc_id's (or atleast all doc_id's from index1) that have the status as "active" in index2 and age 20 or more than 5 sessions in index1? Kinda like a join operation?

I tried a multisearch query but I would have to write a decent chunk of code to join and filter the results the way I need so I`m trying to create a more efficient process, if that's not possible I'll have to use multisearch

Have you tried using "should" for that?
"should" would return any of the matching field. So it works like "or" condition.

you mean something like this?

{
  "query": {
    "bool": {
      "must" : [
        {
          "bool": {
            "should": [
              {"match": {"status": "active"}}
            ]
          }
        },
        {
          "bool": {
            "should" : [
              {
                "match": {"age": 20}
              },
              {
                "range": {
                  "sessions": {"gte": 5}
                }
              }
            ]
          }
        }
      ],
      "must_not" : [
        {
          "match": {
            "company_id": 2
          }
        }
      ]
    }
  }
}

I tried it and it didn't work

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.