# Query where fields from different indexes are not all equal?

**URL:** <https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174>\
**Category:** Elasticsearch\
**Created:** [July 21, 2017, 11:43pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174 "2017-07-21T23:43:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [July 21, 2017, 11:43pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/1 "2017-07-21T23:43:08Z")

</div>

Hi,

Can i query across 2 indexes with just 2 common fields and get other fields in the results ?  
example :  
Index 1 has fields : A, B, C, D  
Index 2 has fields : A,B,E,F,G

Only A and B are common between these indexes, however i want all the fields in the response.  
example my result should contain : A,B,C,D,E,F,G

Currently when i run the query, i only get A,B and don't see the rest of the fields.

Let me know how this is possible.

---

<div class="post-metadata">

**Author:** ![geekpete](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/geekpete/32/20409_2.png) [@geekpete](https://discuss.elastic.co/u/geekpete)\
**Post date:** [July 26, 2017, 12:47pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/2 "2017-07-26T12:47:50Z")

</div>

Hi Sam,

A couple of quick examples, you can test these using Dev Console in Kibana if you want.

```auto
# index two docs into two indices - index1/index2
# a field in one doc in each index has a different value to the rest of the fields in that doc, to demonstrate a search example
POST /index1/doc
{
  "A": "testing1 sample",
  "B": "testing2 sample",
  "C": "testing3 sample",
  "D": "testing4 sample"
}

POST /index1/doc
{
  "A": "testing1 sample",
  "B": "testing2 sample",
  "C": "testing3 sample",
  "D": "testing4 only D has this"
}

POST /index2/doc
{
  "A": "testing1 sample",
  "B": "testing2 sample",
  "E": "testing3 sample",
  "F": "testing4 sample",
  "G": "testing5 sample"
}

POST /index2/doc
{
  "A": "testing1 sample",
  "B": "testing2 sample",
  "E": "testing3 sample",
  "F": "testing4 sample",
  "G": "testing5 only G has this"
}

# search both indices for any docs with any fields containing the word "sample"
GET /index1,index2/_search
{
  "query": {
    "match": {
      "_all": "sample"
    }
  }
}

# search both indices for any docs with any field containing the word "only"
GET /index1,index2/_search
{
  "query": {
    "match": {
      "_all": "only"
    }
  }
}

#multi_match query allows you to list the fields you might want to search
GET /index1,index2/_search
{
  "query": {
    "multi_match" : {
      "query": "sample",
      "fields": ["A", "B", "C", "D", "E", "F", "G"] 
    }
  }
}

# search for "only" with multi_match
GET /index1,index2/_search
{
  "query": {
    "multi_match" : {
      "query": "only",
      "fields": ["A", "B", "C", "D", "E", "F", "G"] 
    }
  }
}

# use the _all field with multi_match, note that _all will be deprecated and off by default in 6.0 - https://www.elastic.co/guide/en/elasticsearch/reference/master/mapping-all-field.html
GET /index1,index2/_search
{
  "query": {
    "multi_match" : {
      "query": "only",
      "fields": ["_all"] 
    }
  }
}

```

Not sure if you meant your issue was with searching or with the results returned.

Can you provide an example doc with an example search?

---

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [August 1, 2017, 6:28pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/4 "2017-08-01T18:28:59Z")

</div>

Thanks a lot geekpete, but how to filter on multi\_match? i am trying below Query and it is failing.

```
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "iPhone",
          "type": "most_fields",
          "fields": [
            "comment",
            "content.main.title"
          ]
        }
      },
      "filter": {
        "term": {
          "locale": "en_US"
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![geekpete](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/geekpete/32/20409_2.png) [@geekpete](https://discuss.elastic.co/u/geekpete)\
**Post date:** [August 2, 2017, 6:17am UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/5 "2017-08-02T06:17:36Z")

</div>

How is it failing? It's just not returning the expected results?

The way you've used the bool filter with the bool must multi\_match appears correct.  
Can you test only your filter to ensure that's working as expected.  
Then test only your multi\_match query to ensure that's working as expected.

So testing just the filter:

```auto
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "locale": "en_US"
        }
      }
    }
  }
}

```

Testing just the multi\_match:

```auto
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "iPhone",
          "type": "most_fields",
          "fields": [
            "comment",
            "content.main.title"
          ]
        }
      }
    }
  }
}

```

And do you see expected results from those separate queries?

So using the example docs I had above, the same type of queries with those doc would be:

```auto
# test multi_match for "testing3" in a set of fields
GET /index1,index2/_search
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "testing3",
          "fields": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G"
          ]
        }
      }
    }
  }
}

```

```auto
# Now test the filter by itself, only docs with "only" in a "G" field
GET /index1,index2/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "G": "only"
        }
      }
    }
  }
}

```

```auto
# now combine the known working multi_match query with the working filter
GET /index1,index2/_search
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "testing3",
          "fields": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G"
          ]
        }
      },
      "filter": {
        "term": {
          "G": "only"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [August 3, 2017, 5:59pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/6 "2017-08-03T17:59:30Z")

</div>

```
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "locale": "en_US"
        }
      }
    }
  }
}

```

The Above Query is not working for some reason, but multi\_match is working fine.

---

<div class="post-metadata">

**Author:** ![samdash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samdash/32/21628_2.png) [@samdash](https://discuss.elastic.co/u/samdash)\
**Post date:** [August 4, 2017, 6:24pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/7 "2017-08-04T18:24:00Z")

</div>

It is working fine now, looks like locale:"en\_US" is case sensitive, after giving locale:"en\_us" it is working fine.  
Appreciate you help thanks a lot.

---

<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:** [September 1, 2017, 6:24pm UTC](https://discuss.elastic.co/t/query-where-fields-from-different-indexes-are-not-all-equal/94174/8 "2017-09-01T18:24:19Z")

</div>

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