[[analysis-keep-types-tokenfilter]]
=== Keep Types Token Filter

A token filter of type `keep_types` that only keeps tokens with a token type
contained in a predefined set.


[float]
=== Options
[horizontal]
types:: a list of types to include (default mode) or exclude
mode:: if set to `include` (default) the specified token types will be kept, 
if set to `exclude` the specified token types will be removed from the stream

[float]
=== Settings example

You can set it up like:

[source,js]
--------------------------------------------------
PUT /keep_types_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "extract_numbers"]
                }
            },
            "filter" : {
                "extract_numbers" : {
                    "type" : "keep_types",
                    "types" : [ "<NUM>" ]
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE

And test it like:

[source,js]
--------------------------------------------------
POST /keep_types_example/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "this is just 1 a test"
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

The response will be:

[source,js]
--------------------------------------------------
{
  "tokens": [
    {
      "token": "1",
      "start_offset": 13,
      "end_offset": 14,
      "type": "<NUM>",
      "position": 3
    }
  ]
}
--------------------------------------------------
// TESTRESPONSE

Note how only the `<NUM>` token is in the output.

=== Exclude mode settings example

If the `mode` parameter is set to `exclude` like in the following example:

[source,js]
--------------------------------------------------
PUT /keep_types_exclude_example
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "remove_numbers"]
                }
            },
            "filter" : {
                "remove_numbers" : {
                    "type" : "keep_types",
                    "mode" : "exclude",
                    "types" : [ "<NUM>" ]
                }
            }
        }
    }
}
--------------------------------------------------
// CONSOLE

And we test it like:

[source,js]
--------------------------------------------------
POST /keep_types_exclude_example/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "hello 101 world"
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

The response will be:

[source,js]
--------------------------------------------------
{
  "tokens": [
    {
      "token": "hello",
      "start_offset": 0,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 0
    }, 
    {
      "token": "world",
      "start_offset": 10,
      "end_offset": 15,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}
--------------------------------------------------
// TESTRESPONSE