Terms facets and filters

Mapping

{
  "id": { "type": "keyword" },
  "title": { "type": "text" },
  "tag": { "type": "text_keyword" }
}

Documents

{
  "documents": [
    {
      "id": "1",
      "title": "Harry Potter and the Philosopher's Stone",
      "tag": "Fantasy"
    },
    {
      "id": "2",
      "title": "Harry Potter and the Chamber of Secrets",
      "tag": ["Fantasy", "Mystery"]
    },
    {
      "id": "3",
      "title": "The Hunger Games",
      "tag": ["Fantasy", "Young Adult", "20% Discount"]
    },
    {
      "id": "4",
      "title": "A Game of Thrones - A Song of Ice and Fire, Book 1",
      "tag": "Fantasy"
    },
    { "id": "5", "title": "Shutter Island", "tag": "Mystery" },
    {
      "id": "6",
      "title": "Harry Potter and the Half-Blood Prince",
      "tag": ["Fantasy", "Young Adult"]
    },
    {
      "id": "7",
      "title": "Harry Potter and the Goblet of Fire",
      "tag": ["Fantasy", "Young Adult", "20% Discount"]
    },
    {
      "id": "8",
      "title": "Harry Potter and the Order of the Phoenix",
      "tag": ["20% Discount"]
    },
    {
      "id": "9",
      "title": "The Harry Potter Encyclopedia",
      "tag": ["20% Discount", "Encyclopedias"]
    }
  ]
}

Query Configuration

{
  "description": "Book query",
  "configuration": {
    "queryFields": {
      "title": 5,
      "tag": 2
    },
    "match": "all",
    "selectFields": ["id", "title"],
    "facets": [
      {
        "type": "terms",
        "key": "tag",
        "label": "Tag",
        "limit": 10
      }
    ]
  },
  "debugMode": true
}

Public Query

Facets

Request:

{
  "searchText": "harry potter"
}

Response:

{
  "searchText": "harry potter",
  "total": 6,
  "items": [
    {
      "id": "9",
      "title": "The Harry Potter Encyclopedia"
    },
    {
      "id": "1",
      "title": "Harry Potter and the Philosopher's Stone"
    },
    {
      "id": "2",
      "title": "Harry Potter and the Chamber of Secrets"
    },
    {
      "id": "6",
      "title": "Harry Potter and the Half-Blood Prince"
    },
    {
      "id": "7",
      "title": "Harry Potter and the Goblet of Fire"
    },
    {
      "id": "8",
      "title": "Harry Potter and the Order of the Phoenix"
    }
  ],
  "facets": [
    {
      "key": "tag",
      "label": "Tag",
      "items": [
        {
          "title": "Fantasy",
          "count": 4
        },
        {
          "title": "20% Discount",
          "count": 3
        },
        {
          "title": "Young Adult",
          "count": 2
        },
        {
          "title": "Encyclopedias",
          "count": 1
        },
        {
          "title": "Mystery",
          "count": 1
        }
      ],
      "type": "terms"
    }
  ],
  "filters": {},
  "metadata": {}
}

Filter by facet term value

Request:

{
  "searchText": "harry potter",
  "filters": {
    "tag": ["Encyclopedias"]
  }
}

Response:

{
  "searchText": "harry potter",
  "total": 1,
  "items": [
    {
      "id": "9",
      "title": "The Harry Potter Encyclopedia"
    }
  ],
  "facets": [
    {
      "key": "tag",
      "label": "Tag",
      "items": [
        {
          "title": "Fantasy",
          "count": 4
        },
        {
          "title": "20% Discount",
          "count": 3
        },
        {
          "title": "Young Adult",
          "count": 2
        },
        {
          "title": "Encyclopedias",
          "count": 1
        },
        {
          "title": "Mystery",
          "count": 1
        }
      ],
      "type": "terms"
    }
  ],
  "filters": {
    "tag": ["Encyclopedias"]
  },
  "metadata": {}
}

Filter by multiple term values

Request:

{
  "searchText": "harry potter",
  "filters": {
    "tag": ["Encyclopedias", "Young Adult"]
  }
}

Response:

{
  "searchText": "harry potter",
  "total": 3,
  "items": [
    {
      "id": "9",
      "title": "The Harry Potter Encyclopedia"
    },
    {
      "id": "6",
      "title": "Harry Potter and the Half-Blood Prince"
    },
    {
      "id": "7",
      "title": "Harry Potter and the Goblet of Fire"
    }
  ],
  "facets": [
    {
      "key": "tag",
      "label": "Tag",
      "items": [
        {
          "title": "Fantasy",
          "count": 4
        },
        {
          "title": "20% Discount",
          "count": 3
        },
        {
          "title": "Young Adult",
          "count": 2
        },
        {
          "title": "Encyclopedias",
          "count": 1
        },
        {
          "title": "Mystery",
          "count": 1
        }
      ],
      "type": "terms"
    }
  ],
  "filters": {
    "tag": ["Encyclopedias", "Young Adult"]
  },
  "metadata": {}
}