summaryrefslogtreecommitdiff
path: root/docs/reference/search/suggesters/context-suggest.asciidoc
blob: 064f919c25d3694f0e48e1c67a24dfe335088783 (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
[[suggester-context]]
=== Context Suggester

The completion suggester considers all documents in the index, but it is often
desirable to serve suggestions filtered and/or boosted by some criteria.
For example, you want to suggest song titles filtered by certain artists or
you want to boost song titles based on their genre.

To achieve suggestion filtering and/or boosting, you can add context mappings while
configuring a completion field. You can define multiple context mappings for a
completion field.
Every context mapping has a unique name and a type. There are two types: `category`
and `geo`. Context mappings are configured under the `contexts` parameter in
the field mapping.

The following defines types, each with two context mappings for a completion
field:

[source,js]
--------------------------------------------------
PUT place
{
    "mappings": {
        "shops" : {
            "properties" : {
                "suggest" : {
                    "type" : "completion",
                    "contexts": [
                        { <1>
                            "name": "place_type",
                            "type": "category"
                        },
                        { <2>
                            "name": "location",
                            "type": "geo",
                            "precision": 4
                        }
                    ]
                }
            }
        }
    }
}
PUT place_path_category
{
    "mappings": {
        "shops" : {
            "properties" : {
                "suggest" : {
                    "type" : "completion",
                    "contexts": [
                        { <3>
                            "name": "place_type",
                            "type": "category",
                            "path": "cat"
                        },
                        { <4>
                            "name": "location",
                            "type": "geo",
                            "precision": 4,
                            "path": "loc"
                        }
                    ]
                },
                "loc": {
                    "type": "geo_point"
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TESTSETUP
<1> Defines a `category` context named 'place_type' where the categories must be
    sent with the suggestions.
<2> Defines a `geo` context named 'location' where the categories must be sent
    with the suggestions.
<3> Defines a `category` context named 'place_type' where the categories are
    read from the `cat` field.
<4> Defines a `geo` context named 'location' where the categories are read from
    the `loc` field.

NOTE: Adding context mappings increases the index size for completion field. The completion index
is entirely heap resident, you can monitor the completion field index size using <<indices-stats>>.

[[suggester-context-category]]
[float]
==== Category Context

The `category` context allows you to associate one or more categories with suggestions at index
time. At query time, suggestions can be filtered and boosted by their associated categories.

The mappings are set up like the `place_type` fields above. If `path` is defined
then the categories are read from that path in the document, otherwise they must
be sent in the suggest field like this:

[source,js]
--------------------------------------------------
PUT place/shops/1
{
    "suggest": {
        "input": ["timmy's", "starbucks", "dunkin donuts"],
        "contexts": {
            "place_type": ["cafe", "food"] <1>
        }
    }
}
--------------------------------------------------
// CONSOLE
<1> These suggestions will be associated with 'cafe' and 'food' category.

If the mapping had a `path` then the following index request would be enough to
add the categories:

[source,js]
--------------------------------------------------
PUT place_path_category/shops/1
{
    "suggest": ["timmy's", "starbucks", "dunkin donuts"],
    "cat": ["cafe", "food"] <1>
}
--------------------------------------------------
// CONSOLE
<1> These suggestions will be associated with 'cafe' and 'food' category.

NOTE: If context mapping references another field and the categories
are explicitly indexed, the suggestions are indexed with both set
of categories.


[float]
===== Category Query

Suggestions can be filtered by one or more categories. The following
filters suggestions by multiple categories:

[source,js]
--------------------------------------------------
POST place/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "place_type": [ "cafe", "restaurants" ]
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

NOTE: When no categories are provided at query-time, all indexed documents are considered.
Querying with no categories on a category enabled completion field should be avoided, as it
will degrade search performance.

Suggestions with certain categories can be boosted higher than others.
The following filters suggestions by categories and additionally boosts
suggestions associated with some categories:

[source,js]
--------------------------------------------------
POST place/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "place_type": [ <1>
                        { "context" : "cafe" },
                        { "context" : "restaurants", "boost": 2 }
                     ]
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[continued]
<1> The context query filter suggestions associated with
    categories 'cafe' and 'restaurants' and boosts the
    suggestions associated with 'restaurants' by a
    factor of `2`

In addition to accepting category values, a context query can be composed of
multiple category context clauses. The following parameters are supported for a
`category` context clause:

[horizontal]
`context`::
    The value of the category to filter/boost on.
    This is mandatory.

`boost`::
    The factor by which the score of the suggestion
    should be boosted, the score is computed by
    multiplying the boost with the suggestion weight,
    defaults to `1`

`prefix`::
    Whether the category value should be treated as a
    prefix or not. For example, if set to `true`,
    you can filter category of 'type1', 'type2' and
    so on, by specifying a category prefix of 'type'.
    Defaults to `false`

[[suggester-context-geo]]
[float]
==== Geo location Context

A `geo` context allows you to associate one or more geo points or geohashes with suggestions
at index time. At query time, suggestions can be filtered and boosted if they are within
a certain distance of a specified geo location.

Internally, geo points are encoded as geohashes with the specified precision.

[float]
===== Geo Mapping

In addition to the `path` setting, `geo` context mapping accepts the following settings:

[horizontal]
`precision`::
    This defines the precision of the geohash to be indexed and can be specified
    as a distance value (`5m`, `10km` etc.), or as a raw geohash precision (`1`..`12`).
    Defaults to a raw geohash precision value of `6`.

NOTE: The index time `precision` setting sets the maximum geohash precision that
can be used at query time.

[float]
===== Indexing geo contexts

`geo` contexts can be explicitly set with suggestions or be indexed from a geo point field in the
document via the `path` parameter, similar to `category` contexts. Associating multiple geo location context
with a suggestion, will index the suggestion for every geo location. The following indexes a suggestion
with two geo location contexts:

[source,js]
--------------------------------------------------
PUT place/shops/1
{
    "suggest": {
        "input": "timmy's",
        "contexts": {
            "location": [
                {
                    "lat": 43.6624803,
                    "lon": -79.3863353
                },
                {
                    "lat": 43.6624718,
                    "lon": -79.3873227
                }
            ]
        }
    }
}
--------------------------------------------------
// CONSOLE

[float]
===== Geo location Query

Suggestions can be filtered and boosted with respect to how close they are to one or
more geo points. The following filters suggestions that fall within the area represented by
the encoded geohash of a geo point:

[source,js]
--------------------------------------------------
POST place/_search
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "location": {
                        "lat": 43.662,
                        "lon": -79.380
                    }
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

NOTE: When a location with a lower precision at query time is specified, all suggestions
that fall within the area will be considered.

Suggestions that are within an area represented by a geohash can also be boosted higher
than others, as shown by the following:

[source,js]
--------------------------------------------------
POST place/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "location": [ <1>
                        {
                            "lat": 43.6624803,
                            "lon": -79.3863353,
                            "precision": 2
                        },
                        {
                            "context": {
                                "lat": 43.6624803,
                                "lon": -79.3863353
                            },
                            "boost": 2
                        }
                     ]
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE
// TEST[continued]
<1> The context query filters for suggestions that fall under
    the geo location represented by a geohash of '(43.662, -79.380)'
    with a precision of '2' and boosts suggestions
    that fall under the geohash representation of '(43.6624803, -79.3863353)'
    with a default precision of '6' by a factor of `2`

In addition to accepting context values, a context query can be composed of
multiple context clauses. The following parameters are supported for a
`category` context clause:

[horizontal]
`context`::
    A geo point object or a geo hash string to filter or
    boost the suggestion by. This is mandatory.

`boost`::
    The factor by which the score of the suggestion
    should be boosted, the score is computed by
    multiplying the boost with the suggestion weight,
    defaults to `1`

`precision`::
    The precision of the geohash to encode the query geo point.
    This can be specified as a distance value (`5m`, `10km` etc.),
    or as a raw geohash precision (`1`..`12`).
    Defaults to index time precision level.

`neighbours`::
    Accepts an array of precision values at which
    neighbouring geohashes should be taken into account.
    precision value can be a distance value (`5m`, `10km` etc.)
    or a raw geohash precision (`1`..`12`). Defaults to
    generating neighbours for index time precision level.