# Multi-index query returning no results

**URL:** <https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855>\
**Category:** Elasticsearch\
**Created:** [August 1, 2023, 8:19pm UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855 "2023-08-01T20:19:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PedroD](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@PedroD](https://discuss.elastic.co/u/PedroD)\
**Post date:** [August 1, 2023, 8:19pm UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/1 "2023-08-01T20:19:37Z")

</div>

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

```auto
{
  "_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

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

```

Query

```auto
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

---

<div class="post-metadata">

**Author:** ![linkerc](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@linkerc](https://discuss.elastic.co/u/linkerc)\
**Post date:** [August 1, 2023, 10:12pm UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/2 "2023-08-01T22:12:52Z")

</div>

"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.

---

<div class="post-metadata">

**Author:** ![PedroD](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@PedroD](https://discuss.elastic.co/u/PedroD)\
**Post date:** [August 1, 2023, 10:22pm UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/3 "2023-08-01T22:22:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![linkerc](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@linkerc](https://discuss.elastic.co/u/linkerc)\
**Post date:** [August 1, 2023, 10:34pm UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/4 "2023-08-01T22:34:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PedroD](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@PedroD](https://discuss.elastic.co/u/PedroD)\
**Post date:** [August 2, 2023, 12:01am UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/5 "2023-08-02T00:01:47Z")

</div>

you mean something like this?

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 30, 2023, 12:02am UTC](https://discuss.elastic.co/t/multi-index-query-returning-no-results/339855/6 "2023-08-30T00:02:25Z")

</div>

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