> ## Documentation Index
> Fetch the complete documentation index at: https://docs.enterspeed.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Filter Expressions

Filter expressions are used when doing lookups in JSON or JavaScript schemas.

Below is a list of examples of what you can do with the filter.

<Tip>
  If you are using JavaScript and your filter contains expressions, it's often easier to use string interpolation instead of string concatination. See [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#string_interpolation) .
</Tip>

<Info>
  If you are comparing values containing URI special characters, you should URI encode the value in your filter expression. See [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent).

  ```javascript theme={null}
  filter(`originParentId eq '${encodeURIComponent(sourceEntity.originId)}'`)
  ```
</Info>

## Examples

Examples of supported binary operators & expressions:

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="EQUALS operator" theme={null}
    // Property value equal to constant string value
    originId eq '100'

    // Property value equal to expressed string value
    originId eq '${sourceEntity.properties.selectedOtherPageId}'

    // Property value is null
    originParentId eq null

    // Property value equal to integer value
    properties.price eq 9

    // Property value equal to decimal value
    properties.price eq 9.99

    // Property value equal to boolean value (true/false)
    properties.isFeatured eq true
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="EQUALS operator" theme={null}
    // Property value equal to constant string value
    originId eq '100'

    // Property value equal to expressed string value
    originId eq '{p.selectedOtherPageId}'

    // Property value is null
    originParentId eq null

    // Property value equal to integer value
    properties.price eq 9

    // Property value equal to decimal value
    properties.price eq 9.99

    // Property value equal to boolean value (true/false)
    properties.isFeatured eq true
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="NOT EQUALS operator" theme={null}
    // Property value not equal to constant string value
    originId ne '100'

    // Property value not equal to expressed string value
    originId ne '${sourceEntity.properties.selectedOtherPageId}'

    // Property value is not null
    originParentId ne null

    // Property value not equal to integer value
    properties.price ne 9

    // Property value not equal to decimal value
    properties.price ne 9.99

    // Property value not equal to boolean value (true/false)
    properties.isFeatured ne true
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="NOT EQUALS operator" theme={null}
    // Property value not equal to constant string value
    originId ne '100'

    // Property value not equal to expressed string value
    originId ne '{p.selectedOtherPageId}'

    // Property value is not null
    originParentId ne null

    // Property value not equal to integer value
    properties.price ne 9

    // Property value not equal to decimal value
    properties.price ne 9.99

    // Property value not equal to boolean value (true/false)
    properties.isFeatured ne true
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="AND operator" theme={null}
    type eq 'article' and properties.isFeatured eq true
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="AND operator" theme={null}
    type eq 'article' and properties.isFeatured eq true
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="OR operator" theme={null}
    type eq 'article' or type eq 'contentPage'
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="OR operator" theme={null}
    type eq 'article' or type eq 'contentPage'
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="An array contains any matching value:" theme={null}
    // Contains specific redirect with constant string value
    redirects/any(r: r eq '/old-page')

    // Contains specific tags with expression string value
    properties.tags/any(t: t eq '${sourceEntity.properties.selectedTag}')

    // Contains articles that feature, matching constant boolean value
    properties.articles/any(a: a.isFeatured eq true)
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="An array contains any matching value:" theme={null}
    // Contains specific redirect with constant string value
    redirects/any(r: r eq '/old-page')

    // Contains specific tags with expression string value
    properties.tags/any(t: t eq '{p.selectedTag}')

    // Contains articles that feature, matching constant boolean value
    properties.articles/any(a: a.isFeatured eq true)
    ```
  </Tab>
</Tabs>

<Info>
  Please note that our filter expressions only support one use of `any` per expression.\
  E.g. `properties.tags/any(t: t eq 'tag1') or properties.tags/any(t: t eq 'tag2')` is not supported.
</Info>

<Tabs>
  <Tab title="JavaScript">
    ```javascript title="Check whether the element exists in this collection" theme={null}
    // Finds matches in predefined integers array
    properties.favoriteId in (100, 200)

    // Finds matches in predefined strings array
    properties.color in ('blue', 'red', 'green')

    // Finds matches in favorites selection (array of integers)
    properties.favoriteId in (${sourceEntity.properties.favoriteIds})

    // Finds matches in colors selection (array of strings)
    properties.color in (${sourceEntity.properties.colors.map(c => `'${c}'`)})
    ```
  </Tab>

  <Tab title="JSON">
    ```javascript title="Check whether the element exists in this collection" theme={null}
    // Finds matches in predefined integers array
    properties.favoriteId in (100, 200)

    // Finds matches in predefined strings array
    properties.color in ('blue', 'red', 'green')

    // Finds matches in favorites selection (array of integers)
    properties.favoriteId in {properties.favoriteIds}

    // Finds matches in colors selection (array of strings)
    properties.color in {properties.selectedColors}
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="JavaScript">
    ```js title="Full example" theme={null}
    export default {
      // ...
      properties: function (sourceEntity, context) {
        return {
            // ...
            sportArticles: context
                    .reference('featuredSportArticle')
                    .filter(`type eq 'article' and properties.tags/any(t: t eq '${sourceEntity.properties.tag}')`)
                    .orderBy({ propertyName: 'properties.metaData.sortOrder', direction: "asc"})
                    .limit(5)
        }
      },
    };
    ```
  </Tab>

  <Tab title="JSON">
    ```json title="Full example" theme={null}
    "sportArticles": {
        "type": "array",
        "input": {
            "$lookup": {
                "filter": "type eq 'article' and properties.tags/any(t: t eq '{p.tag}')",
                "orderBy": {
                  "property": "properties.metaData.sortOrder",
                  "sort": "asc"
                },
                "top": 5
            }
        },
        "var": "article",
        "items": {
            "type": "object",
            "properties": {
                "title": "{article.p.title}",
                "content": "{article.p.content}"
            }
        }
    }
    ```
  </Tab>
</Tabs>
