# Filter by multiple fields in one document (one field more than other field)

**URL:** <https://discuss.elastic.co/t/filter-by-multiple-fields-in-one-document-one-field-more-than-other-field/258427>\
**Category:** Elasticsearch\
**Created:** [December 11, 2020, 3:34pm UTC](https://discuss.elastic.co/t/filter-by-multiple-fields-in-one-document-one-field-more-than-other-field/258427 "2020-12-11T15:34:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delagardi1](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Delagardi1](https://discuss.elastic.co/u/Delagardi1)\
**Post date:** [December 11, 2020, 3:34pm UTC](https://discuss.elastic.co/t/filter-by-multiple-fields-in-one-document-one-field-more-than-other-field/258427/1 "2020-12-11T15:34:33Z")

</div>

Hi!  
I am pretty new to elasticsearch. Is there is a way to filter my search results by comparing two values in one document?

I have two fields in my document: inStock, inOtherStock and 3 filter values like: show me all, show me only negative, only positive.

So if  
'show me all', I should get all data without filter  
'show me only negative' i should get only documents where inStock \< inOtherStock  
'only positive' I should get only docs where inStock \> inOtherStock

My search looks like this:

```auto
(search, filterValue)

return {
      body: {
        query: {
          query_string: {
            query: search`,
            fields: [
              'name',
              'sku',
              'bundleId',
              'orders.poNumber',
              'otherIndex.ownerEmail',
              'otherIndex.modifiedProductName',
              'inStock',
              'inOtherStock',
            ],
            analyzer: 'standard',
          },
        },
        sort: [{ _id: { order: 'desc' } }],
      },
    };

```

---

<div class="post-metadata">

**Author:** ![Delagardi1](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Delagardi1](https://discuss.elastic.co/u/Delagardi1)\
**Post date:** [December 11, 2020, 5:11pm UTC](https://discuss.elastic.co/t/filter-by-multiple-fields-in-one-document-one-field-more-than-other-field/258427/2 "2020-12-11T17:11:10Z")

</div>

I have done this in next way. Is there are a way to make it better?

```auto
export const getFilterScript = (quantityDiff) => {
  switch (quantityDiff) {
    case PRODUCTS_QUANTITY_FILTER.SHORTAGE: {
      return {
        script: {
          script: "doc['inStock'].value > doc['inOtherStock'].value"
        }
      }
    }

    case PRODUCTS_QUANTITY_FILTER.OVERRAGE: {
      return {
        script: {
          script: "doc['inStock'].value < doc['inOtherStock'].value"
        }
      }
    }

    default: {
      return {
        script: {
          script: "return true"
        }
      }
    }
  }
}

```

```auto
// quantityDiff = 'show me all' | 'show me only negative' | 'only positive' 

const filter = getFilterScript(quantityDiff); 

return {
      body: {
        query: {
          bool: {
            must: [
              {
                ...filter,
              },
              {
                query_string: {
                  query: search,
                  fields: [
                   'name',
                   'sku',
                   'bundleId',
                   'orders.poNumber',
                   'otherIndex.ownerEmail',
                   'otherIndex.modifiedProductName',
                   'inStock',
                   'inOtherStock',
                  ],
                  analyzer: 'standard',
                },
              }
            ]
          },
        },
        sort: [{ _id: { order: 'desc' } }],
      },
    };

```

---

<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:** [January 8, 2021, 5:11pm UTC](https://discuss.elastic.co/t/filter-by-multiple-fields-in-one-document-one-field-more-than-other-field/258427/3 "2021-01-08T17:11:45Z")

</div>

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