Back to main page

Search

The search API is used make SQL like selections in aito

Description

Aito's search query structure resembles the SQL query structure.

First, you specify the examined table in the 'from' field. You can for example state, that you are looking at the impression table.

Second, you can declare a set of conditions in the 'where' field. For example, you may declare, that the customer id is 4, and the query is laptop. The returned results will fulfil these conditions

Third, you may declare the order the results are results. You may e.g. order the results, by the displayed product id.

Fourth, you may select the displayed fields. The fields may belong to the examined table, like 'query', or they may belong to linked tables such as 'user.name' and 'product.title'.

Fifth, you may page the results, using the offset and the limit fields. By default, aito returns only 10 results in order to limit server memory consumption and avoid slow responses with huge datasets (there may be thousands or millions of matches).

API end point

/api/v1/_search

Format:

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

See also:

Normal Search Query

Give me 3 laptops, ordered by price. Include only specific fields in results.

POST /api/v1/_search

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

Result

    {
      "offset" : 0,
      "total" : 3,
      "hits" : [ {
        "id" : 3,
        "title" : "apple macbook",
        "price" : 1500.0
      }, {
        "id" : 4,
        "title" : "hp spectre",
        "price" : 1500.0
      }, {
        "id" : 5,
        "title" : "lenovo ideapad white",
        "price" : 400.0
      } ]
    }

Or Operation

Give me phones and phone covers. Show title and tags.

POST /api/v1/_search

    {
      "from" : "products",
      "where": {
        "tags" :
          {"$or" : [
            {"$match" : "phone"},
            {"$match" : "cover"}
          ]}
      },
      "select" : ["title", "tags"]
    }

Result

    {
      "offset" : 0,
      "total" : 6,
      "hits" : [ {
        "title" : "apple iphone",
        "tags" : "ios phone premium"
      }, {
        "title" : "samsung s8",
        "tags" : "android phone premium"
      }, {
        "title" : "huawei honor",
        "tags" : "android phone affordable"
      }, {
        "title" : "iphone protection white",
        "tags" : "cover"
      }, {
        "title" : "iphone protection leather",
        "tags" : "cover"
      }, {
        "title" : "samsung s8 leather cover",
        "tags" : "cover"
      } ]
    }

And operation

Give me products in price range 300-800.

POST /api/v1/_search

    {
      "from" : "products",
      "where": {
        "price":
          {"$and" : [
            {"$gte":300},
            {"$lte":800}
          ]}
      }
    }

Result

    {
      "offset" : 0,
      "total" : 3,
      "hits" : [ {
        "description" : "apple iphone is a premium phone",
        "id" : 0,
        "name" : "iphone",
        "price" : 800.0,
        "tags" : "ios phone premium",
        "title" : "apple iphone"
      }, {
        "description" : "samsung s8 is a premium phone with all sorts of features ",
        "id" : 1,
        "name" : "s8",
        "price" : 600.0,
        "tags" : "android phone premium",
        "title" : "samsung s8"
      }, {
        "description" : "lenovo idea pad is an affordable white laptop",
        "id" : 5,
        "name" : "ideapad",
        "price" : 400.0,
        "tags" : "windows laptop affordable",
        "title" : "lenovo ideapad white"
      } ]
    }