2013-10-08 07:55:25 -04:00
|
|
|
[[suggester-context]]
|
2014-05-21 14:47:25 -04:00
|
|
|
=== Context Suggester
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
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.
|
|
|
|
|
2018-09-12 02:47:32 -04:00
|
|
|
NOTE: It is mandatory to provide a context when indexing and querying
|
|
|
|
a context enabled completion field.
|
|
|
|
|
2016-08-03 18:40:17 -04:00
|
|
|
The following defines types, each with two context mappings for a completion
|
|
|
|
field:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2019-01-14 16:08:01 -05:00
|
|
|
PUT place?include_type_name=true
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2016-08-03 18:40:17 -04:00
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc" : {
|
2016-08-03 18:40:17 -04:00
|
|
|
"properties" : {
|
|
|
|
"suggest" : {
|
|
|
|
"type" : "completion",
|
|
|
|
"contexts": [
|
|
|
|
{ <1>
|
|
|
|
"name": "place_type",
|
2017-05-26 11:41:40 -04:00
|
|
|
"type": "category"
|
2016-08-03 18:40:17 -04:00
|
|
|
},
|
|
|
|
{ <2>
|
|
|
|
"name": "location",
|
|
|
|
"type": "geo",
|
|
|
|
"precision": 4
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 16:08:01 -05:00
|
|
|
PUT place_path_category?include_type_name=true
|
2016-08-03 18:40:17 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc" : {
|
2016-08-03 18:40:17 -04:00
|
|
|
"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"
|
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-06 10:46:50 -04:00
|
|
|
// CONSOLE
|
2016-08-03 18:40:17 -04:00
|
|
|
// 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.
|
2016-08-03 20:20:00 -04:00
|
|
|
<3> Defines a `category` context named 'place_type' where the categories are
|
2016-08-03 18:40:17 -04:00
|
|
|
read from the `cat` field.
|
2016-08-03 20:20:00 -04:00
|
|
|
<4> Defines a `geo` context named 'location' where the categories are read from
|
2016-08-03 18:40:17 -04:00
|
|
|
the `loc` field.
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
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>>.
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[[suggester-context-category]]
|
|
|
|
[float]
|
2014-05-21 14:47:25 -04:00
|
|
|
==== Category Context
|
2015-10-31 01:59:21 -04:00
|
|
|
|
|
|
|
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.
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2016-08-03 18:40:17 -04:00
|
|
|
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:
|
2015-10-31 01:59:21 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT place/_doc/1
|
2015-10-31 01:59:21 -04:00
|
|
|
{
|
|
|
|
"suggest": {
|
|
|
|
"input": ["timmy's", "starbucks", "dunkin donuts"],
|
2016-01-20 09:41:56 -05:00
|
|
|
"contexts": {
|
2015-10-31 01:59:21 -04:00
|
|
|
"place_type": ["cafe", "food"] <1>
|
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
2015-10-31 01:59:21 -04:00
|
|
|
<1> These suggestions will be associated with 'cafe' and 'food' category.
|
|
|
|
|
2016-08-03 18:40:17 -04:00
|
|
|
If the mapping had a `path` then the following index request would be enough to
|
|
|
|
add the categories:
|
2015-10-31 01:59:21 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT place_path_category/_doc/1
|
2015-10-31 01:59:21 -04:00
|
|
|
{
|
|
|
|
"suggest": ["timmy's", "starbucks", "dunkin donuts"],
|
|
|
|
"cat": ["cafe", "food"] <1>
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
2015-10-31 01:59:21 -04:00
|
|
|
<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.
|
|
|
|
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[float]
|
2015-10-31 01:59:21 -04:00
|
|
|
===== Category Query
|
|
|
|
|
|
|
|
Suggestions can be filtered by one or more categories. The following
|
|
|
|
filters suggestions by multiple categories:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST place/_search?pretty
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"place_suggestion" : {
|
|
|
|
"prefix" : "tim",
|
|
|
|
"completion" : {
|
|
|
|
"field" : "suggest",
|
|
|
|
"size": 10,
|
|
|
|
"contexts": {
|
|
|
|
"place_type": [ "cafe", "restaurants" ]
|
|
|
|
}
|
2015-10-31 01:59:21 -04:00
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2018-09-12 02:47:32 -04:00
|
|
|
NOTE: If multiple categories or category contexts are set on the query
|
|
|
|
they are merged as a disjunction. This means that suggestions match
|
|
|
|
if they contain at least one of the provided context values.
|
2015-10-31 01:59:21 -04:00
|
|
|
|
|
|
|
Suggestions with certain categories can be boosted higher than others.
|
|
|
|
The following filters suggestions by categories and additionally boosts
|
|
|
|
suggestions associated with some categories:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST place/_search?pretty
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"place_suggestion" : {
|
|
|
|
"prefix" : "tim",
|
|
|
|
"completion" : {
|
|
|
|
"field" : "suggest",
|
|
|
|
"size": 10,
|
|
|
|
"contexts": {
|
|
|
|
"place_type": [ <1>
|
|
|
|
{ "context" : "cafe" },
|
|
|
|
{ "context" : "restaurants", "boost": 2 }
|
|
|
|
]
|
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
2015-10-31 01:59:21 -04:00
|
|
|
<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`
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2018-09-12 02:47:32 -04:00
|
|
|
NOTE: If a suggestion entry matches multiple contexts the final score is computed as the
|
|
|
|
maximum score produced by any matching contexts.
|
|
|
|
|
2013-10-08 07:55:25 -04:00
|
|
|
[[suggester-context-geo]]
|
|
|
|
[float]
|
2014-05-21 14:47:25 -04:00
|
|
|
==== Geo location Context
|
2015-10-31 01:59:21 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-11-08 03:51:14 -05:00
|
|
|
Internally, geo points are encoded as geohashes with the specified precision.
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[float]
|
2015-10-31 01:59:21 -04:00
|
|
|
===== Geo Mapping
|
|
|
|
|
|
|
|
In addition to the `path` setting, `geo` context mapping accepts the following settings:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[horizontal]
|
2015-10-31 01:59:21 -04:00
|
|
|
`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.
|
|
|
|
|
2013-10-08 07:55:25 -04:00
|
|
|
[float]
|
2015-10-31 01:59:21 -04:00
|
|
|
===== Indexing geo contexts
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
`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:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT place/_doc/1
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2015-10-31 01:59:21 -04:00
|
|
|
"suggest": {
|
|
|
|
"input": "timmy's",
|
2016-08-03 18:40:17 -04:00
|
|
|
"contexts": {
|
2015-10-31 01:59:21 -04:00
|
|
|
"location": [
|
|
|
|
{
|
|
|
|
"lat": 43.6624803,
|
|
|
|
"lon": -79.3863353
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lat": 43.6624718,
|
|
|
|
"lon": -79.3873227
|
|
|
|
}
|
|
|
|
]
|
2016-08-03 18:40:17 -04:00
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[float]
|
2014-05-21 14:47:25 -04:00
|
|
|
===== Geo location Query
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
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:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST place/_search
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"place_suggestion" : {
|
|
|
|
"prefix" : "tim",
|
|
|
|
"completion" : {
|
|
|
|
"field" : "suggest",
|
|
|
|
"size": 10,
|
|
|
|
"contexts": {
|
|
|
|
"location": {
|
|
|
|
"lat": 43.662,
|
|
|
|
"lon": -79.380
|
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-03 18:40:17 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
NOTE: When a location with a lower precision at query time is specified, all suggestions
|
|
|
|
that fall within the area will be considered.
|
|
|
|
|
2018-09-12 02:47:32 -04:00
|
|
|
NOTE: If multiple categories or category contexts are set on the query
|
|
|
|
they are merged as a disjunction. This means that suggestions match
|
|
|
|
if they contain at least one of the provided context values.
|
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
Suggestions that are within an area represented by a geohash can also be boosted higher
|
|
|
|
than others, as shown by the following:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-12-14 21:49:53 -05:00
|
|
|
POST place/_search?pretty
|
2013-10-08 07:55:25 -04:00
|
|
|
{
|
2016-12-14 21:49:53 -05:00
|
|
|
"suggest": {
|
|
|
|
"place_suggestion" : {
|
|
|
|
"prefix" : "tim",
|
|
|
|
"completion" : {
|
|
|
|
"field" : "suggest",
|
|
|
|
"size": 10,
|
|
|
|
"contexts": {
|
|
|
|
"location": [ <1>
|
|
|
|
{
|
2015-10-31 01:59:21 -04:00
|
|
|
"lat": 43.6624803,
|
2016-12-14 21:49:53 -05:00
|
|
|
"lon": -79.3863353,
|
|
|
|
"precision": 2
|
2015-10-31 01:59:21 -04:00
|
|
|
},
|
2016-12-14 21:49:53 -05:00
|
|
|
{
|
|
|
|
"context": {
|
|
|
|
"lat": 43.6624803,
|
|
|
|
"lon": -79.3863353
|
|
|
|
},
|
|
|
|
"boost": 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2013-10-08 07:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-06 10:46:50 -04:00
|
|
|
// CONSOLE
|
2016-08-03 18:40:17 -04:00
|
|
|
// TEST[continued]
|
2015-10-31 01:59:21 -04:00
|
|
|
<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`
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2018-09-12 02:47:32 -04:00
|
|
|
NOTE: If a suggestion entry matches multiple contexts the final score is computed as the
|
|
|
|
maximum score produced by any matching contexts.
|
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
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:
|
2013-10-08 07:55:25 -04:00
|
|
|
|
2015-10-31 01:59:21 -04:00
|
|
|
[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.
|