Back to main page

Query

Generic queries can be used to provide the functionality of most other functionality in the API

Description

Generic Aito queries are a more powerful expert tool, that essentially provide the functionality of every other query type in the Aito api. Search, similarity, match and recommend cann be seen as convenience APIs for the generic query.

Generic Aito query resembles the 'search' query, except that it supports 'get' statement. Get statement can be used in queries such as:

{
  "from" : "impressions",
  "where": {
    "customer" : 0,
    "query"    : "best laptop"
  },
  "get":"product",
  "orderBy": {"$p": {"$context" : {"click":true } } },
  "select": ["title", "$highlight"],
  "limit":2
}

This query 'gets' a product, that maximizes the click likelihood for given user & query. In essence, this generic query is a recommendation query.

Note: that 'get' operation changes the namespaces of 'select' and 'orderBy' operations. The namespace is changed from context table to the linked table. E.g. if you start from the impression table, and get a linked product, use "select":["title"] instead of "select":["product.title"] and use "orderBy":["price"] instead of "orderBy":["product"].

API end point

/api/v1/_query

Format:

    {
      "from" : From, 
      "where" : null | Proposition, 
      "get" : null | Get, 
      "orderBy" : null | OrderBy, 
      "select" : null | Selection, 
      "offset" : null | long, 
      "limit" : null | long
    }

See also:

Generic Search Query

Following query returns the laptop products

POST /api/v1/_query

    {
      "from" : "products",
      "where": {
        "tags" : { "$match": "laptop" }
      },
      "select": ["title"],
      "orderBy": "price"
    }

Result

    {
      "offset" : 0,
      "total" : 3,
      "hits" : [ {
        "title" : "apple macbook"
      }, {
        "title" : "hp spectre"
      }, {
        "title" : "lenovo ideapad white"
      } ]
    }

Generic Recommendation Query

Following query returns the products, that maximizes click through rate for user 0 and query 'best laptop'.

POST /api/v1/_query

    {
      "from" : "impressions",
      "where": {
        "customer" : 0,
        "query"    : "best laptop"
      },
      "get":"product",
      "orderBy": {"$p": {"$context" : {"click":true } } },
      "select": ["title", "$highlight"],
      "limit":2
    }

Result

    {
      "offset" : 0,
      "total" : 11,
      "hits" : [ {
        "title" : "apple macbook",
        "$highlight" : [ {
          "score" : 2.2883414733152936,
          "field" : "description",
          "highlight" : "apple macbook <font color=\"green\">is</font> the top <font color=\"green\">laptop</font> <font color=\"green\">in</font> the market"
        }, {
          "score" : 1.6688953121558205,
          "field" : "title",
          "highlight" : "<font color=\"green\">apple</font> macbook"
        }, {
          "score" : 1.6129244719554066,
          "field" : "price",
          "highlight" : "<font color=\"green\">1500.0</font>"
        }, {
          "score" : 1.5931175830386215,
          "field" : "tags",
          "highlight" : "macosx <font color=\"green\">laptop</font> <font color=\"green\">premium</font>"
        }, {
          "score" : 1.1834864148959776,
          "field" : "id",
          "highlight" : "<font color=\"green\">3</font>"
        } ]
      }, {
        "title" : "hp spectre",
        "$highlight" : [ {
          "score" : 3.0720222145004503,
          "field" : "tags",
          "highlight" : "<font color=\"green\">windows</font> <font color=\"green\">laptop</font> <font color=\"green\">premium</font>"
        }, {
          "score" : 1.6129244719554066,
          "field" : "price",
          "highlight" : "<font color=\"green\">1500.0</font>"
        }, {
          "score" : 1.5878279554228414,
          "field" : "description",
          "highlight" : "hp spectre <font color=\"green\">is</font> <font color=\"green\">a</font> premium <font color=\"green\">laptop</font>, that <font color=\"green\">is</font> compatible with phones"
        } ]
      } ]
    }

Generic Match Query

Match a product to the customer's wish.

POST /api/v1/_query

    {
       "from" : "messages",
       "where": {
         "message" : "Recommend me a premium laptop."
       },
       "get" : "product",
       "orderBy" : "$p",
       "select": ["title", "$highlight"],
       "limit" : 2
    }

Result

The above query returns an array of results, sorted by probability

    {
      "offset" : 0,
      "total" : 11,
      "hits" : [ {
        "title" : "hp spectre",
        "$highlight" : [ {
          "score" : 3.242274624661655,
          "field" : "description",
          "highlight" : "hp spectre <font color=\"green\">is</font> <font color=\"green\">a</font> premium <font color=\"green\">laptop</font>, that <font color=\"green\">is</font> compatible with <font color=\"red\">phones</font>"
        }, {
          "score" : 2.015373985784721,
          "field" : "tags",
          "highlight" : "windows <font color=\"green\">laptop</font> <font color=\"green\">premium</font>"
        }, {
          "score" : 0.632294986071229,
          "field" : "id",
          "highlight" : "<font color=\"green\">4</font>"
        } ]
      }, {
        "title" : "apple macbook",
        "$highlight" : [ {
          "score" : 2.1047655907782765,
          "field" : "description",
          "highlight" : "apple macbook <font color=\"green\">is</font> the top <font color=\"green\">laptop</font> in the market"
        }, {
          "score" : 2.015373985784721,
          "field" : "tags",
          "highlight" : "macosx <font color=\"green\">laptop</font> <font color=\"green\">premium</font>"
        } ]
      } ]
    }