Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

SETINDEX

Josh Baker edited this page Oct 11, 2016 · 18 revisions

SETINDEX name pattern ...

Set an index. This operation will replace an existing index with the same name. The second parameter is a pattern that is used to filter on keys.

A index allows for ordering by values rather than keys. A non-spatial index is implemented as a B-tree structure that supports fast and stable retrieval of values. A spatial index is implemented as an R-tree structure for handling values that operate in multiple-dimensions.

There are four major types of indexes that SummitDB supports.

  • Text - text and binary
  • Number - int64, uint64, and floats
  • JSON - document fields. Similar to MongoDB
  • User-defined - User-defined indexes using Javascript
  • Spatial - multi-dimensional for geospatial, time, and ranges

SETINDEX name pattern TEXT [CS] [COLLATE collate] [DESC]

Set an index ordered by text or binary. This operation will replace an existing index with the same name. The second parameter is a pattern that is used to filter on keys.

Options

  • CS - case-sensitive ordering, useful for raw binary
  • COLLATE - collate i18n ordering. This is similar to the SQL COLLATE keyword found in traditional databases
  • DESC - order descending

Return value

Simple string reply: OK if SETINDEX was executed correctly.

Examples

Let's say you want to create an index for ordering names:

> SETINDEX names user:*:name TEXT
OK
> SET user:0:name tom
OK
> SET user:1:name Randi
OK
> SET user:2:name jane
OK
> SET user:4:name Janet
OK
> SET user:5:name Paula
OK
> SET user:6:name peter
OK
> SET user:7:name Terri
OK
> ITER names
 1) "user:2:name"
 2) "jane"
 3) "user:4:name"
 4) "Janet"
 5) "user:5:name"
 6) "Paula"
 7) "user:6:name"
 8) "peter"
 9) "user:1:name"
10) "Randi"
11) "user:7:name"
12) "Terri"
13) "user:0:name"
14) "tom"

SETINDEX name pattern INT|UINT|FLOAT [DESC]

Set an index ordered numerically. This operation will replace an existing index with the same name. The second parameter is a pattern that is used to filter on keys.

Options

  • INT - use 64-bit signed integer
  • UINT - use 64-bit unsigned integer
  • FLOAT - use 64-bit floating point
  • DESC - order descending

Return value

Simple string reply: OK if SETINDEX was executed correctly.

Examples

To create an index that is numerically ordered on an age key, we could use:

> SETINDEX ages user:*:age INT
OK
> SET user:0:age 35
OK
> SET user:1:age 49
OK
> SET user:2:age 13
OK
> SET user:4:age 63
OK
> SET user:5:age 8
OK
> SET user:6:age 3
OK
> SET user:7:age 16
OK
> ITER ages
 1) "user:6:name" 
 2) "3"
 3) "user:5:name"
 4) "8"
 5) "user:2:name"
 6) "13"
 7) "user:7:name"
 8) "16"
 9) "user:0:name"
10) "35"
11) "user:1:name"
12) "49"
13) "user:4:name"
14) "63"

SETINDEX index pattern JSON path [CS] [COLLATE collate] [DESC] [...]

Set an index ordered by a value in a JSON document. This operation will replace an existing index with the same name. The second parameter is a pattern that is used to filter on keys.

The path parameter points to a JSON field, and uses GJSON path syntax.

There's also support for multi value indexes. Similar to a SQL multi column index. This can be done by chaining indexes together like:

SETINDEX last_name_age user:* JSON name.last JSON age

Options

  • CS - case-sensitive ordering, useful for raw binary
  • COLLATE - collate i18n ordering. This is similar to the SQL COLLATE keyword found in traditional databases
  • DESC - order descending

Return value

Simple string reply: OK if SETINDEX was executed correctly.

Examples

Single value index:

> SETINDEX last_name * JSON name.last
OK
> SET user:1 '{"name":{"first":"Tom","last":"Johnson"},"age":38}'
OK
> SET user:2 '{"name":{"first":"Janet","last":"Prichard"},"age":47}'
OK
> SET user:3 '{"name":{"first":"Carol","last":"Anderson"},"age":52}'
OK
> SET user:4 '{"name":{"first":"Alan","last":"Cooper"},"age":28}'
OK
> ITER last_name
1) "user:3"
2) "{\"name\":{\"first\":\"Carol\",\"last\":\"Anderson\"},\"age\":52}"
3) "user:4"
4) "{\"name\":{\"first\":\"Alan\",\"last\":\"Cooper\"},\"age\":28}"
5) "user:1"
6) "{\"name\":{\"first\":\"Tom\",\"last\":\"Johnson\"},\"age\":38}"
7) "user:2"
8) "{\"name\":{\"first\":\"Janet\",\"last\":\"Prichard\"},\"age\":47}"

Multi value index:

> SETINDEX last_name_age * JSON name.last JSON age
OK
> SET user:1 '{"name":{"first":"Tom","last":"Johnson"},"age":38}'
OK
> SET user:2 '{"name":{"first":"Janet","last":"Prichard"},"age":47}'
OK
> SET user:3 '{"name":{"first":"Carol","last":"Anderson"},"age":52}'
OK
> SET user:4 '{"name":{"first":"Alan","last":"Cooper"},"age":28}'
OK
> SET user:5 '{"name":{"first":"Sam","last":"Anderson"},"age":51}'
OK
> SET user:6 '{"name":{"first":"Melinda","last":"Prichard"},"age":44}'
OK
> ITER last_name_age
 1) "user:5"
 2) "{\"name\":{\"first\":\"Sam\",\"last\":\"Anderson\"},\"age\":51}"
 3) "user:3"
 4) "{\"name\":{\"first\":\"Carol\",\"last\":\"Anderson\"},\"age\":52}"
 5) "user:4"
 6) "{\"name\":{\"first\":\"Alan\",\"last\":\"Cooper\"},\"age\":28}"
 7) "user:1"
 8) "{\"name\":{\"first\":\"Tom\",\"last\":\"Johnson\"},\"age\":38}"
 9) "user:6"
10) "{\"name\":{\"first\":\"Melinda\",\"last\":\"Prichard\"},\"age\":44}"
11) "user:2"
12) "{\"name\":{\"first\":\"Janet\",\"last\":\"Prichard\"},\"age\":47}"

SETINDEX name pattern EVAL function [DESC]

Set an index ordered by a user-defined function. This operation will replace an existing index with the same name. The second parameter is a pattern that is used to filter on keys.

The function parameter is a Javascript function which uses a signature that takes two arguments and returns a boolean. A return value of true means that the first argument is less-than the second argument.

The most simplest example is:

function(a, b) { return a < b; }

It could also be much more complex, for example here's how to compare Social Security Numbers:

function(a, b){
    var aparts = a.split('-');
    var bparts = b.split('-');
    for (var i=0;i<3;i++){
        if (parseInt(aparts[i]) < parseInt(bparts[i])){
            return true
        }
        if (parseInt(aparts[i]) > parseInt(bparts[i])){
            return false
        }
    }
    return false
}

Return value

Simple string reply: OK if SETINDEX was executed correctly.

Examples

Order by social security number:

> SETINDEX ssn user:*:ssn EVAL "function(a, b){var aparts=a.split('-');var bparts=b.split('-');for (var i=0;i<3;i++){if (parseInt(aparts[i])<parseInt(bparts[i])){return true;}if (parseInt(aparts[i])>parseInt(bparts[i])){return false;}}return false;}"
OK
> set user:1:ssn 483-23-1234
OK
> set user:2:ssn 903-12-8735
OK
> set user:3:ssn 120-77-9812
OK
> ITER ssn
1) "user:3:ssn"
2) "120-77-9812"
3) "user:1:ssn"
4) "483-23-1234"
5) "user:2:ssn"
6) "903-12-8735"

Please note that the example above puts the javascript function on one line so that it's easy to cut-and-paste into the redis-cli. Multi-line javascript is supported with standard clients, such as redigo.

SETINDEX name pattern SPATIAL [JSON path]

Set an index for storing rectangles in an R-tree. An R-tree is organized in a similar manner as a B-tree, and both are balanced trees. Except an R-tree is special because it can operate on data that is in multiple dimensions. This is super handy for Geospatial applications.

The values must be in one of the following formats:

  • BuntDB Rect - A simple format that defines multi-dimensional rectangles
  • WKT - A common textual format that is used in many SQL databases. Supports Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, and GeometryCollection.
  • GeoJSON - A popular JSON format that is similar to WKT. It's used by many applications such as MongoDB

The JSON path option tells the index to use a field in a JSON document rather than the entire value. The path uses GJSON path syntax. For example:

SETINDEX pos trucks:* SPATIAL JSON pos

Tells the index to use the "pos" value inside a JSON document such as:

{
  "driver": "Janet",
  "speed": 35,
  "pos": {"type":"Point","coordinates":[-115.121,33.897]}
}

Or:

{
  "driver": "Janet",
  "speed": 35,
  "pos": "[-115.121 33.897]"
}

Return value

Simple string reply: OK if SETINDEX was executed correctly.

Examples

Using BuntDB Rects:

> SETINDEX fleet fleet:*:pos SPATIAL
OK
> SET fleet:0:pos "[-115.567 33.532]"
OK
> SET fleet:1:pos "[-121.896 32.334]"
OK
> SET fleet:2:pos "[-116.671 35.735]"
OK
> SET fleet:3:pos "[-113.902 31.234]"
OK
> RECT fleet "[-117 30],[-112 36]"
1) "fleet:0:pos"
2) "[-115.567 33.532]"
3) "fleet:2:pos"
4) "[-116.671 35.735]"
5) "fleet:3:pos"
6) "[-113.902 31.234]"

Using nested GeoJSON:

> SETINDEX fleet fleet:* SPATIAL JSON pos
OK
> SET fleet:0 '{"driver":"Janet","pos":{"type":"Point","coordinates":[-115.567,33.532]}}'
OK
> SET fleet:1 '{"driver":"Tom","pos":{"type":"Point","coordinates":[-121.896,32.334]}}'
OK
> SET fleet:2 '{"driver":"Andrew","pos":{"type":"Point","coordinates":[-116.671,35.735]}}'
OK
> SET fleet:3 '{"driver":"Pam","pos":{"type":"Point","coordinates":[-113.902,31.234]}}'
OK
> RECT fleet '{"pos":"[-117 30],[-112 36]"}'
1) "fleet:0"
2) "{\"driver\":\"Janet\",\"pos\":{\"type\":\"Point\",\"coordinates\":[-115.567,33.532]}}"
3) "fleet:2"
4) "{\"driver\":\"Andrew\",\"pos\":{\"type\":\"Point\",\"coordinates\":[-116.671,35.735]}}"
5) "fleet:3"
6) "{\"driver\":\"Pam\",\"pos\":{\"type\":\"Point\",\"coordinates\":[-113.902,31.234]}}"

Related commands

DELINDEX, INDEXES, ITER, RECT, SETINDEX

Clone this wiki locally