SonarW Indexes

SonarW does not support indexes in the MongoDB sense; it indexes every field automatically using a Big Data index optimized for very large data size, without the need for large memory footprint. Sonar does not limit the size of an indexed field.

The only exceptions are the Text Index and Word Index features.

Text Index

SonarW optimizes the speed of text search using a text search engine.

To create a text index:

db.txt.drop()
db.txt.insert({a: "abc", b: 1})
db.txt.insert({a: "abc", b: 2})
db.txt.createIndex({"a":"text"})

This creates a text index which can later be used optimize text searches. Multiple columns within the given collection may be text indexed in this way. SonarW does not support wildcard column names using a text index.

To drop a text index from collection txt:

db.txt.dropIndex({"a":"text"})

This drops a previously created text index.

To drop all text indexes:

db.runCommand({dropIndexes: 1})

This drops all previously created text indexes.

Or, you can delete all previously created text indexes:

db.runCommand({deleteIndexes: 1})

This deletes all previously created text indexes.

SonarW accepts but ignores the reIndex command.

Unsupported $text index features:

$language: none
$diacriticSensitive: true

Unsupported $text index options:

default_language: always defaults to english

Word Index

Word Index is a feature for SonarW that allows you to create an index of the words contained within a field or an entire document, and run a query against the index, achieving faster results than by using other query methods.

The basic Word Index is:

{"*": "word"}

The Word Index query operation ($word) is:

{$word: "< The word to be queried >"}

This query can be used along with other queries when performing a find or match within a collection.

Parameters

The default parameter values for the Word Index are shown below:

  • Delimiters:

    ;,\t\n\'\")(][%@!:&^=+-
    
  • Case sensitive: False

  • Minimum characters: 3

  • Maximum characters: 255

  • Stop Words (words to be excluded from index): None

You can expand/modify the default parameters for the Word Index as shown below:

delimiters: “< a string with delimiters >”,

or

additional_delimiters: “< a string with delimiters to be added to the default >”

case_sensitive: < true or false >

min_chars: < integer greater than 1 >

max_chars: < integer less than 256 >

stop_words: [ “< words to be excluded from the index, separated by commas >” ]

These parameters can be modified during the creation of a new Word Index; an example is shown below:

> db.collection.createIndex({"*": "word"}, {case_sensitve: true,
min_chars: 10})

Examples

The examples described in this section will use the following data:

> db.dogs.find()

{ "_id" : ObjectId("5cf055877c4c5fda8beb320c"), "name" : "Benji",
"breed" : "mixed", "color" : "brown", "age" : 6 }

{ "_id" : ObjectId("5cf055887c4c5fda8beb320d"), "name" : "Rover",
"breed" : "beagle", "color" : "brown and white", "age" : 14 }

{ "_id" : ObjectId("5cf055de7c4c5fda8beb320e"), "name" : "Rudy",
"breed" : "terrier", "color" : "black and white", "age" : 11 }

{ "_id" : ObjectId("5cf057bcc74f35801544bf6e"), "name" : "James
Brown", "breed" : "golden retriever", "color" : "golden", "age" : 2 }

{ "_id" : ObjectId("5cf05e613e1e51407d9773f7"), "name" : "Sprite",
"breed" : "mixed", "color" : "brown and orange", "age" : 12 }
  1. To create a basic word index of the entire document:

    > db.dogs.createIndex({"*": "word"})
    

The above command will build an index as per the default parameters. The basic Word Index is case insensitive by default; a $word query for the word “Brown” (capitalized) against this index will return the following results:

> db.dogs.find({$word: "Brown"})

{ "_id" : ObjectId("5cf055877c4c5fda8beb320c"), "name" : "Benji",
"breed" : "mixed", "color" : "brown", "age" : 6 }

{ "_id" : ObjectId("5cf055887c4c5fda8beb320d"), "name" : "Rover",
"breed" : "beagle", "color" : "brown and white", "age" : 14 }

{ "_id" : ObjectId("5cf057bcc74f35801544bf6e"), "name" : "James
Brown", "breed" : "golden retriever", "color" : "golden", "age" : 2 }

{ "_id" : ObjectId("5cf05e613e1e51407d9773f7"), "name" : "Sprite",
"breed" : "mixed", "color" : "brown and orange", "age" : 12 }

Note: Performing this same $word query – for the word “Brown” (capitalized) – against a case-sensitive index would return the following result:

> db.dogs.createIndex({"*": "word"}, {case_sensitive: true})

> db.dogs.find({$word: "Brown"})

{ "_id" : ObjectId("5cf057bcc74f35801544bf6e"), "name" : "James
Brown", "breed" : "golden retriever", "color" : "golden", "age" : 2 }
  1. To define a Word Index that indexes only words in the “breed” and “color” fields:

    > db.dogs.createIndex({"breed": "word", "color": "word"})
    

This will build an index of all words within the specified fields only. If you run a $word query for the word “brown” against this index, it will return the following results:

> db.dogs.find({$word: "brown"})

{ "_id" : ObjectId("5cf055877c4c5fda8beb320c"), "name" : "Benji",
"breed" : "mixed", "color" : "brown", "age" : 6 }

{ "_id" : ObjectId("5cf055887c4c5fda8beb320d"), "name" : "Rover",
"breed" : "beagle", "color" : "brown and white", "age" : 14 }

{ "_id" : ObjectId("5cf05e613e1e51407d9773f7"), "name" : "Sprite",
"breed" : "mixed", "color" : "brown and orange", "age" : 12 }
  1. To create a basic Word Index that contains only words consisting of 10 or more characters:

    > db.dogs.createIndex({"*": "word"}, {min_chars: 10})
    
  2. You can also perform a $word query in conjunction with other queries (e.g. with the “greater than” operator). The index/query example shown below will create a basic word index and return documents that satisfy all of the following criteria:

    • Contain the word “brown”
    • Contain an integer with a value greater than 10 in the “age” field

Example:

> db.dogs.createIndex({"*": "word"})

> db.dogs.find({$and:[{$word: "brown"}, {age: {$gt: 10}}]})

{ "_id" : ObjectId("5cf055887c4c5fda8beb320d"), "name" : "Rover",
"breed" : "beagle", "color" : "brown and white", "age" : 14 }

{ "_id" : ObjectId("5cf05e613e1e51407d9773f7"), "name" : "Sprite",
"breed" : "mixed", "color" : "brown and orange", "age" : 12 }