summaryrefslogtreecommitdiffstats
path: root/SEARCH.md
blob: cc6d074c92bc0b197a75db004ad14787b0cf5e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# Search Requests

## Overview
Ultimately, the point of storing documents in a document store is to be able to access the data embodied in those documents in a variety of interesting ways.  To put it another way, we want to be able to ask interesting questions of the data that has been persisted, not just get back a document that we put in.

We do this by submitting _Search Requests_ to the _Search Service_.

Conceptually, the structure of a _Search Request_ can be visualized as a pipeline consisting of three phases of operations that are applied to our document set in order to produce a query result set:

    +---------------------+
    | ENTIRE DOCUMENT SET | ----------------------------- We begin with the entire set of documents in our index.
    +---------------------+
               |
               V
        \-------------/          
         \ Filter(s) / ---------------------------------- We optionally apply a set of filter criteria to the document 
          -----------                                     set, with the intent of reducing the overall set of documents 
               |                                          that we will be applying our query against (filtering is cheaper 
               |                                          than running the full queries)
               V                   
      +-----------------+        
      | DOCUMENT SUBSET | ------------------------------- Our filter stage produces a (hopefully) smaller subset of documents 
      +-----------------+                                 than we started with.
               |
               V
         \-----------/      
          \ Queries / ----------------------------------- Now we execute our queries against the filtered document subset.
           --------- 
               |
               V
     +------------------+          
     | QUERY RESULT SET | ------------------------------- This produces a scored set of results. 
     +------------------+            
               |                     
               V                                
      \----------------/             
       \ Aggregations / --------------------------------- Optionally we may apply a set of aggregations against the query               
        --------------                                    result set.
               |                     
               V                     
    +---------------------+          
    | AGGREGATION BUCKETS | ----------------------------- This produces a set of aggregation buckets based on the query
    +---------------------+                               result set.



### Filters
_Filters_ are intended to answer the following question about the documents that they are applied to:  _Does this document match the specified criteria?_

In other words, filter queries produce **yes/no** results.  A given document either **IS** or **IS NOT** in the set of documents that we want.  This can also be described as a _non-scored_ search and is typically a cheaper operation than performing a _scored_ search (more on those below).  This is why we often want to pre-filter our document set before applying more expensive query operations.

### Queries
_Queries_ are intended to answer the following question about the documents that they are applied to: _How well does this document match the specified criteria?_ 

In other words, the criteria which we include in our _Query_ is not intended to produce a set of _yes/no_ results, but a _scored_ result set which includes the set of documents that meet some combination of the criteria which we supply and includes a _score_ indicating _how well_ a particular document meets the overall requirement set.  The more criteria that a particular document meets, the higher its score.

### Aggregations
_Aggregations_ are intended to answer questions that summarize and analyze an overall data set (typically a _Query_ result set).

_Aggregations_ produce result sets that group the data set into buckets, based on the _Aggregation_ criteria, and allow questions such as the following to be asked:

* Of all of the people in my result set, how many are taller than 5' 10"?
* How many different server vendors have hardware installed in my network?
* What proportion of employees serviced by our IT department use Macs?  What proportion use Windows?

## Syntax

### The Filter Stanza

If you intend for your _Search Request_ to include a _Filter_ stage, then you must define the _filter queries_ that you wish to be applied in the _Filter Stanza_ of your _Search Request_.  The following pseudo-json illustrates the structure of the _Filter Stanza_:

    {
        "filter": {
            "all": [ {filter-query}, {filter-query}, ... {filter-query}],
            "any": [ {filter-query}, {filter-query}, ... {filter-query}]
        }
    }

As we can see, our _Filter Stanza_ consists of two optional groupings of _query_ statements: _any_ and _all_.  These groupings will produce the following behaviours:

* _all_ - _Documents_ must match ALL of the _queries_ in this grouping in order to be included in the result set.
* _any_ - _Documents_ must match a minimum of ONE of the _queries_ in this grouping in order to be included in the result set.

The _filter-queries_ themselves are syntactically identical to the _Queries_ which we will define below, the only difference being that, because they are declared within the _Filter Stanza_ they will be applied as _unscored_ queries.

### The Query Stanza

The _Query Stanza_ is where we define _query statements_ which are intended to produce _scored results_, as opposed to _queries_ which are intended for filtering purposes.

The _Query Stanza_ is expressed as a list of _query statements_, each of which is prefixed with a directive indicating how strongly the _query_ in question is to be applied.

_Queries_ prefixed with the "must" directive, represent queries which all documents must satisfy to be included in the result set, whereas, _queries_ prefixed with the "may" directive represent queries which are not required for a document to be included in the result set, although they will score higher if they do.

The following pseudo-json illustrates the structure of the _Query Stanza_:
 
    {
        "queries": [
            { "must": { <query> } },
            { "must": { <query> } },
                 .
                 .
            { "must": { <query> } },
            { "may" : { <query> } },
                 .
                 .
            { "may" : { <query> } }
        ]
    }

**Nested Fields** - If the document to be queried contains nested fields, then these may be referenced by providing the fully qualified field name using dotted notation.  Example: _myFieldName.mySubFieldName_

** Result-Set Restrictions** - In some cases, where the number of hits for a given set of queries is expected to be large, the client may want to restrict the size of the result set that is returned, as well as manipulate which subset of results it gets back.  This can be accomplished with the following optional fields in the Search Statement:
* results-start - Index into the result set to the first document to be returned.
* results-size - The maximum number of documents to be returned in the result set.

Both of these fields are optional - leaving them out implies that the entire result set should be returned to the client.

**IMPORTANT - Note that although the these two fields may be used by the client to get back query results a chunk at a time by resending the same query repeatedly and specifying a different 'results-start' value each time, be aware that this is NOT a transactional operation.  There is no guarantee that changes to the underlying data may not occur in between query calls.  This is NOT intended to be the equivalent of a mechanism such as the 'Scroll API' provided by ElasticSearch or a cursor in a traditional data base.**

We will discuss the specific query types supported below, and then provide some concrete examples.

**Term Query**

A _Term_ query attempts to match the literal value of a field, with no advanced parsing or analysis of the query string.

There are two operations supported by the _Term_ query type:
* match - The contents of the specified field must match the supplied value.
* not-match - The contents of the specified field must NOT match the supplied value.

_Example - Simple Match_

    {
        "match": { "FirstName": "Bob" }
    }
    
_Example - Simple Not-Match_

    {
        "not-match": { "LastName": "Smith" }
    }
    
Note that the term match is applied against the tokenized field contents.

For example, if a field containing the contents: "_the quick brown fox_" was analyzed with a white space tokenizer (this occurs on document creation or update), the following inverted indexes would be created for the field at the time of document insertion:

    the
    quick
    brown
    fox

Meaning that the following term queries would all produce a match for our document:

         {"must": {"match": { "my-field-name": "the"}}}
         {"must": {"match": { "my-field-name": "quick"}}}
         {"must": {"match": { "my-field-name": "brown"}}}
         {"must": {"match": { "my-field-name": "fox"}}}

         
**Multi Field Term Query**

A variant of the _Term_ query described above is the  _Multi Field Term Query_, which, as the name suggests, allows for a _Term_ query to be applied across multiple fields.  The syntax is the same as for a single field term query except that the fields are supplied as a space-delimited list, as in the example below:

    {"must": {"match": {"field": "field1 field2 field3", "value": "blah"}}}

The above query would produce a hit for any documents containing the value "blah" in any of "field1", "field2", or "field3".
 
Note that it is also valid to supply multiple values in the same manner, by supplying a space-delimited list in the "value" field.

The default behaviour in this case is to produce a hit only if there is at least one occurrence of EVERY supplied value in any of the specified fields.

For example, the following query:

    {"must": {"match": {"field": "first_name last_name", "value": "Smith Will"}}}
  
Produces a match for document {"first_name": "Will", "last_name": "Smith"} but not {"first_name": "Will", "last_name": "Shakespeare"}
 
This default behaviour can be overriden by explicitly specifying a boolean operator for the query.  Valid operators are as follows:
* and -	At least one occurrence of every value must be present in one of the specified fields to produce a hit. (This is the default behaviour if no operation is specified).
* or - An occurrence of any of the specified values in any of the specified fields will produce a hit.
 
Restating the previous example with the operator explicitly specified illustrates the difference between the two operations:

_Example - Multi field term query with AND operator explicitly set:_

    {"must": {"match": {"field": "first_name last_name", "value": "Smith Will", "operator": "and"}}}
  
Produces a match for document {"first_name": "Will", "last_name": "Smith"} but not {"first_name": "Will", "last_name": "Shakespeare"} -- Exactly as in our previous example since this is the default behaviour.
  
  
_Example - Multi field term query with OR operator explicitly set:_

    {"must": {"match": {"field": "first_name last_name", "value": "Smith Will", "operator": "or"}}}
  
Produces a match for both documents {"first_name": "Will", "last_name": "Smith"} and {"first_name": "Will", "last_name": "Shakespeare"}

**Parsed Query**

Parsed queries apply a query parser to the supplied query string in order to determine the exact query to apply to the specified field.
The query string is parsed into a series of terms and operators, as described below:

_Terms_

Terms may be any of the following:
* single words
* exact phrases, as denoted by enclosing the phrase in open and close quotations.  Example: "this is my exact phrase"
* regular expressions, as denoted by wrapping the expressing in forward slash ( / ) character.  Example: /joh?n(ath[oa]n)/

Note that a series of terms with no explicit operators will be interpreted as a set of optional values.  For example:

    quick brown fox  would match fields which contain quick OR brown OR fox.

_Operators_

Operators provide for more complexity in terms of the behaviour described by the query string.  The supported operators are as follows:

| Operator | Description                                                              | Example        |
| -------- | ------------------------------------------------------------------------ | -------------- |
| +	       | The term to the right of the operator must be present.	                  | +quick         |
| -	       | The term to the right of the operator must not be present                | -brown         |
| AND	   | Both the terms to the left and right of the operator must be present     |	brown AND fox  |
| OR	   | Either the term to the left or right of the operator must be present     | quick OR brown |
| NOT	   | The term to the right of the operator must not be present (similar to -) | NOT fox        |
 
The following pseudo-json illustrates the structure of a parsed query:

    "parsed-query": {
        "field": "fieldname",      // If this field is not present, then apply the query to ALL fields
        "query-string": "string"
    }


**Range Query**

_Range_ queries match fields whose term value falls within the specified numeric or date range.

For fields containing date types, the default format for that field (as provided in the document schema when the index was created) will be used, unless the client overrides that with their own format string.

A _Range_ query includes one or a combination of operations representing the upper and lower bounds of the range.  The following describes the supported bounds operations:

| Operation | Description            |
| --------- | ---------------------- |
| gt	    | Greater than           |
| gte	    | Greater than or equals |
| lt	    | Less than              |
| lte	    | Less than or equals    |
 
The following pseudo-json describes the structure of a ranged query.

    "range": {
        "field": "fieldname",
        "<<operation>>": numeric-or-date-value,     // where <<operation>> is one of the operations from the table above.
              .
              .
        "<<operation>>": numeric-or-date-value,     // where <<operation>> is one of the operations from the table above.
        "format": "format-string"                   // For date ranges, this allows the client to override the format defined 
                                                    // for the field in the document schema.
        "time-zone": "+1:00"                        // For date ranges, this allows the client to specify a time zone parameter 
                                                    // to be applied to the lower and upper bounds of the query.
    }

### The Aggregations Stanza

**Group By Aggregation**

_Group By_ aggregations create buckets based on the values in a specific field.  These are expressed in the following manner:

_Example - Group by last name, excluding any buckets with less than 2 entries._

    {
        "name": "GroupByLastName",
        "aggregation" : {
            "group-by": {
                "field": "LastName",
                "min-threshold": 2
            }
        }
    }


**Date Range Aggregation**

_Date Range_ aggregations produce counts where date type fields fall in or out of a particular range.

_Example - Return counts of the number of people born before Jan 1, 1972 and the number of people born after Jan 1, 1972_

    {
        "name": "AggregateByBirthdate",
        "aggregation": {
            "date-range": {
                "field": "BirthDate",
                "from": "01-01-1972 00:00:00",
                "to": "01-01-1972 00:00:00"
            }
        }
    }

### Putting It All Together

The following examples illustrate how to construct a complete search statement, putting all of the previous building blocks together, starting with the simplest possible search statement, and building toward more complicated request that combine, filters, scored queries, and aggregations.

_Example - Simple search statement with no filtering or aggregations_

Search Statement:

    {
        "queries": [
            {"must": {"match": {"field": "LastName", "value": "Smith"}}}   
        ]
    }

Response Body:

    {
        "searchResult": {
            "totalHits": 3,
            "hits": [
                {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/8",
            	         "etag": "3",
            	         "content": {
            	             "FirstName": "Will",
            	             "LastName": "Smith"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/2",
            	         "etag": "1",
            	         "content": {
            	             "FirstName": "Bob",
            	             "LastName": "Smith"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/10",
            	         "etag": "7",
            	         "content": {
            	             "FirstName": "Alan",
            	             "LastName": "Smith"
            	         }
            	     }
            	 }
            ]
        }
    }

_Example - Simple search statement with multiple term queries, with no filtering or aggregations_

Search Statement:

    {
        "queries": [
            {"must": {"match": {"field": "LastName", "value": "Smith"}}},
            {"may": {"match": {"field": "FirstName", "value", "Bob"}}},
            {"must": {"not-match": {"field": "FirstName", "value", "Alan"}}}
        ]
    }

Response Body:

    {
        "searchResult": {
            "totalHits": 2,
            "hits": [
                {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/8",
            	         "etag": "3",
            	         "content": {
            	             "FirstName": "Bob",
            	             "LastName": "Smith"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.5,
            	     "document": {
            	         "url": "/indexes/people/documents/2",
            	         "etag": "1",
            	         "content": {
            	             "FirstName": "Will",
            	             "LastName": "Smith"
            	         }
            	     }
            	 }
            ]
        }
    }
    
_Example - Simple search statement with a filter stanza_

Search Statement:

    {
        "filter": {
            "all": {
                { "must": {"not-match": {"field": "FirstName", "value", "Bob"}}}
            }
        },
        
        "queries": [
            {"must": {"match": {"field": "LastName", "value": "Smith"}}},
        ]
    }

Response Body:

    {
        "searchResult": {
            "totalHits": 2,
            "hits": [
                {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/8",
            	         "etag": "3",
            	         "content": {
            	             "FirstName": "Will",
            	             "LastName": "Smith"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/10",
            	         "etag": "7",
            	         "content": {
            	             "FirstName": "Alan",
            	             "LastName": "Smith"
            	         }
            	     }
            	 }
            ]
        }
    }

_Example - Simple search statement with filter and aggregation stanzas_

Assuming the following document set:

    {"FirstName": "Will", "LastName": "Smith", "BirthDate": "1968-09-25T00:00:00", "Profession": "Actor"},
    {"FirstName": "Jaden, "LastName": "Smith", "BirthDate": "1998-06-08T00:00:00", "Profession": "Actor"},
    {"FirstName": "Alan, "LastName": "Smith", "BirthDate": "1956-05-21T00:00:00", "Profession": "Artist"},
    {"FirstName": "Wilson", "LastName": "Fisk", "BirthDate": "1962-02-17T00:00:00", "Profession": "Crime Lord"},
    {"FirstName": "Bob", "LastName": "Smith", "BirthDate": "1972-11-05T00:00:00", "Profession": "Plumber"},
    {"FirstName": "Jane", "LastName": "Smith", "BirthDate": "1992-10-15T00:00:00", "Profession": "Accountant"},
    {"FirstName": "John", "LastName": "Doe", "BirthDate": "1981-10-15T00:00:00", "Profession": "Janitor"}
    
Filter out all people born before Jan 1, 1960, then query the remaining set for people who's last name is 'Smith', with preference to plumbers, and finally count the number of each profession in the resulting set:

Search Statement:

    {
        "filter": {
            "all": {
                { "must": {"range": {"field": "BirthDate", "gte", "1960-01-01T00:00:00"}}}
            }
        },
        
        "queries": [
            {"must": {"match": {"field": "LastName", "value": "Smith"}}},
            {"may": {"match": {"field": "Profession", "value": "Plumber"}}}
        ],
        
        "aggregations": [
        	{
        		"name": "by_profession",
        		"aggregation": {
        		    "group-by": {
        		        "field": "Profession"
        		    }
        		}
           } 
        ] 
    }

Response Body:

    {
        "searchResult": {
            "totalHits": 4,
            "hits": [
                {
            	     "score": 0.8,
            	     "document": {
            	         "url": "/indexes/people/documents/8",
            	         "etag": "3",
            	         "content": {
            	             "FirstName": "Bob",
            	             "LastName": "Smith",
            	             "Profession": "Plumber"
            	             "BirthDate": "1972-11-05T00:00:00"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.5,
            	     "document": {
            	         "url": "/indexes/people/documents/10",
            	         "etag": "7",
            	         "content": {
            	             "FirstName": "Will",
            	             "LastName": "Smith",
            	             "Profession": "Actor",
            	             "BirthDate": "1968-09-25T00:00:00"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.5,
            	     "document": {
            	         "url": "/indexes/people/documents/10",
            	         "etag": "7",
            	         "content": {
            	             "FirstName": "Jaden",
            	             "LastName": "Smith",
            	             "Profession": "Actor",
            	             "BirthDate": "1998-06-08T00:00:00"
            	         }
            	     }
            	 },
            	 {
            	     "score": 0.5,
            	     "document": {
            	         "url": "/indexes/people/documents/10",
            	         "etag": "7",
            	         "content": {
            	             "FirstName": "Jane",
            	             "LastName": "Smith",
            	             "Profession": "Accountant",
            	             "BirthDate": "1992-10-15T00:00:00"
            	         }
            	     }
            	 }
            ]
        },
        "aggregationResult": {
            "aggregations": [
                {
                    "name": "by_profession",
                    "buckets": [
                        { "key": "Actor", "count": 2 },
                        { "key": "Plumber", "count": 1 },
                        { "key": "Accountant", "count": 1 }
                    ]
            ]
        }
    }


## API

### Submit a Search Query

---
**URL**

    https://{host}:9509/services/search-data-service/v1/search/indexes/{index}/query/

**Method** 

    POST

**URL Params**

    index - The name of the _Index_ to apply the query against.

**Request Header**

    Accept          = application/json
    X-TransactionId = Unique id set by client (for logging purposes)
    X-FromAppId     = Application identifier (for logging purposes)
    Content-Type    = application/json
    
**Request Payload**

    Search statement expressed in JSON format (see **Syntax**) 

**Success Response**

    Code:      200
    Header(s): None
    Body:      JSON format result set.  
    
**Error Response**

    400 - Bad Request
    403 - Unauthorized
    500 - Internal Error

---