---
title: Query Algebra
slug: user-documentation/query-algebra
docTags: 
createdAt: 2024-04-29T12:29:00.469Z
---

There are multiple query rules you can use to build up any desired condition. Based on this algebra of rules, the search will decide if the query is matching - and thus triggering some action - or not.

Following are a few examples of the query rules algebra to learn about various types and their usage.

## Always

```javascript
{
  "type" : "Always"
}
```

The query always matches.

## Never

```javascript
{
  "type" : "Never"
}
```

The query never matches.

## Match in Specific Field

```javascript
{
  "field" : "manufacturerName",
  "type" : "QueryHasField"
}
```

Query matches if it contains field query on manufacturerName. e.g.: “tshirt && manufacturerName\:hugoboss”

## Match with Specific Value

```javascript
{
  "field" : "siteId",
  "value" : "1234",
  "type" : "QueryHasField"
}
```

Query matches if it contains field siteId with a precise value of “1234”. e.g.: “tshirt && siteId:1234”

## Match if Query Present in Any Field

```javascript
{
  "value" : "iphone",
  "field" : {
    "type" : "AnyField"
  },
  "type" : "QueryHasValue"
}
```

Query matches if there is the term “iphone” in any place. e.g.: “name\:iphone”

## Match without Specified Field Name

```javascript
{
 "value" : "iphone",
 "field" : {
   "type" : "EmptyField"
 },
 "type" : "QueryHasValue"
}
```

Query matches if there is term “iphone”, but only when specified without the field name. e.g.: “iphone || name: mac”

## Match with Either Fields

```javascript
{
  "value" : "iphone",
  "field" : {
    "fields" : [
      "manufacturerName",
      "name"
    ],
    "type" : "FieldNamesSpecified"
  },
  "type" : "QueryHasValue"
}
```

Query matches if there is the term “iphone” in either manufacturerName or name fields. e.g.: “mac || name: iphone”

## Match for Any Field

```javascript
{
  "value" : "computer",
  "field" : {
    "type" : "AnyField"
  },
  "type" : "QueryContainsValue"
}
```

Query matches if part of the query term, in any field, is a “computer”. e.g.: “bestcomputer”

## Match without Specified Fields

```javascript
{
  "value" : "computer",
  "field" : {
    "type" : "EmptyField"
  },
  "type" : "QueryContainsValue"
}
```

Query matches if part of a query term is a “computer”, but only for terms without fields specified.

## Match with Specified Field

```javascript
{
  "value" : "computer",
  "field" : {
    "fields" : [
      "manufacturerName",
      "name"
    ],
    "type" : "FieldNamesSpecified"
  },
  "type" : "QueryContainsValue"
}
```

Query matches if part of a query term is a “computer”, but only for fields manufacturerName and name.

## Match if Present in Any Field

```javascript
{
  "term" : {
    "value" : "tshirts",
    "field" : {
      "type" : "AnyField"
    },
    "type" : "QueryHasValue"
  },
  "type" : "Not"
}
```

Query matches if it doesn’t contain the value “tshirt” in any of its fields. e.g.: “tshirts”. e.g.: “computer“

## Match with Any Field with Multiple Queries

```javascript
{
  "terms" : [
    {
      "value" : "iphone",
      "field" : {
        "type" : "AnyField"
      },
      "type" : "QueryHasValue"
    },
    {
      "value" : "apple",
      "field" : {
        "type" : "AnyField"
      },
      "type" : "QueryContainsValue"
    }
  ],
  "type" : "Or"
}
```

Query matches if it has the term “iphone” in any field or if part of the query contains “apple”.

```javascript
{
  "terms" : [
    {
      "value" : "iphone",
      "field" : {
        "type" : "AnyField"
      },
      "type" : "QueryHasValue"
    },
    {
      "value" : "apple",
      "field" : {
        "fields" : [
          "manufacturerName"
        ],
        "type" : "FieldNamesSpecified"
      },
      "type" : "QueryContainsValue"
    }
  ],
  "type" : "And"
}
```

Query matches if it has the term “iphone” in any field and if the manufacturerName query contains “apple”.
