# Elasticsearch Query: Equal Values of Different Types

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-equal-values-of-different-types/131802>\
**Category:** Elasticsearch\
**Created:** [May 14, 2018, 6:27pm UTC](https://discuss.elastic.co/t/elasticsearch-query-equal-values-of-different-types/131802 "2018-05-14T18:27:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AloysiusParedes](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@AloysiusParedes](https://discuss.elastic.co/u/AloysiusParedes)\
**Post date:** [May 14, 2018, 6:27pm UTC](https://discuss.elastic.co/t/elasticsearch-query-equal-values-of-different-types/131802/1 "2018-05-14T18:27:20Z")

</div>

Hi, new to ELK, and I am ready to start making search queries.

Here is my data (anonymized) and they are all on the same index:

> ```
> {
> "type": "typeA"
> "fieldA1": "valueA1"
> "fieldA2": "valueA2"
> }
> {
> "type": "typeB"
> "fieldB1": "valueB1"
> "fieldB2": "valueB2"
> }
> 
> ```

I am wondering if there is a way to do the query similar to the following SQL Query:

> ```
> SELECT COUNT(b.field1)
> FROM typeA a, typeB b
> WHERE a.fieldA1 = b.fieldB1
> AND a.fieldA2 = "valueA2"
> AND b.fieldB2 = "valueB2"
> 
> ```

I have read from other resources that scripts are the way to go. Any suggestions?

Here is what I have tried:

> ```
> GET /myIndex/_search
> {
> "size": 0,
> "query": {
> "bool": {
> "should": [
> {
> "bool": {
> "must": [
> { "match": { "type": "typeA"} },
> { "match": { "fieldA2": "valueA2" } }
> ]
> }
> },
> {
> "bool": {
> "must": [
> { "match": { "type": "typeB" } },
> { "match": { "fieldB2": "valueB2" } }
> ]
> }
> }
> ] 
> }
> }
> }
> 
> ```

But I don't know what the next steps are for getting the

> ```
> WHERE a.fieldA1 = b.fieldB1
> 
> ```

part of the query.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [May 18, 2018, 8:13pm UTC](https://discuss.elastic.co/t/elasticsearch-query-equal-values-of-different-types/131802/2 "2018-05-18T20:13:32Z")

</div>

There is no query in ES that can do `a.fieldA1 = b.fieldB1` on your flat independent documents. Each document is examined independently if it matches a query or not, and there is no connection to other documents.

Some possible options you may look into:

- [joints](https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html), changing the structure of your documents
- [Terms query with lookup](https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html), with a limitation that a lookup path is limited to a single doc.

---

<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:** [June 15, 2018, 8:13pm UTC](https://discuss.elastic.co/t/elasticsearch-query-equal-values-of-different-types/131802/3 "2018-06-15T20:13:32Z")

</div>

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