This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Delimited term frequency token filter

The delimited_term_freq token filter separates a token stream into tokens with corresponding term frequencies, based on a provided delimiter. A token consists of all characters before the delimiter, and a term frequency is the integer after the delimiter. For example, if the delimiter is |, then for the string foo|5, foo is the token and 5 is its term frequency. If there is no delimiter, the token filter does not modify the term frequency.

You can either use a preconfigured delimited_term_freq token filter or create a custom one.

Preconfigured delimited_term_freq token filter

The preconfigured delimited_term_freq token filter uses the | default delimiter. To analyze text with the preconfigured token filter, send the following request to the _analyze endpoint:

  1. POST /_analyze
  2. {
  3. "text": "foo|100",
  4. "tokenizer": "keyword",
  5. "filter": ["delimited_term_freq"],
  6. "attributes": ["termFrequency"],
  7. "explain": true
  8. }

copy

The attributes array specifies that you want to filter the output of the explain parameter to return only termFrequency. The response contains both the original token and the parsed output of the token filter that includes the term frequency:

  1. {
  2. "detail": {
  3. "custom_analyzer": true,
  4. "charfilters": [],
  5. "tokenizer": {
  6. "name": "keyword",
  7. "tokens": [
  8. {
  9. "token": "foo|100",
  10. "start_offset": 0,
  11. "end_offset": 7,
  12. "type": "word",
  13. "position": 0,
  14. "termFrequency": 1
  15. }
  16. ]
  17. },
  18. "tokenfilters": [
  19. {
  20. "name": "delimited_term_freq",
  21. "tokens": [
  22. {
  23. "token": "foo",
  24. "start_offset": 0,
  25. "end_offset": 7,
  26. "type": "word",
  27. "position": 0,
  28. "termFrequency": 100
  29. }
  30. ]
  31. }
  32. ]
  33. }
  34. }

Custom delimited_term_freq token filter

To configure a custom delimited_term_freq token filter, first specify the delimiter in the mapping request, in this example, ^:

  1. PUT /testindex
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "my_delimited_term_freq": {
  7. "type": "delimited_term_freq",
  8. "delimiter": "^"
  9. }
  10. }
  11. }
  12. }
  13. }

copy

Then analyze text with the custom token filter you created:

  1. POST /testindex/_analyze
  2. {
  3. "text": "foo^3",
  4. "tokenizer": "keyword",
  5. "filter": ["my_delimited_term_freq"],
  6. "attributes": ["termFrequency"],
  7. "explain": true
  8. }

copy

The response contains both the original token and the parsed version with the term frequency:

  1. {
  2. "detail": {
  3. "custom_analyzer": true,
  4. "charfilters": [],
  5. "tokenizer": {
  6. "name": "keyword",
  7. "tokens": [
  8. {
  9. "token": "foo|100",
  10. "start_offset": 0,
  11. "end_offset": 7,
  12. "type": "word",
  13. "position": 0,
  14. "termFrequency": 1
  15. }
  16. ]
  17. },
  18. "tokenfilters": [
  19. {
  20. "name": "delimited_term_freq",
  21. "tokens": [
  22. {
  23. "token": "foo",
  24. "start_offset": 0,
  25. "end_offset": 7,
  26. "type": "word",
  27. "position": 0,
  28. "termFrequency": 100
  29. }
  30. ]
  31. }
  32. ]
  33. }
  34. }

Combining delimited_token_filter with scripts

You can write Painless scripts to calculate custom scores for the documents in the results.

First, create an index and provide the following mappings and settings:

  1. PUT /test
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "analysis": {
  6. "tokenizer": {
  7. "keyword_tokenizer": {
  8. "type": "keyword"
  9. }
  10. },
  11. "filter": {
  12. "my_delimited_term_freq": {
  13. "type": "delimited_term_freq",
  14. "delimiter": "^"
  15. }
  16. },
  17. "analyzer": {
  18. "custom_delimited_analyzer": {
  19. "tokenizer": "keyword_tokenizer",
  20. "filter": ["my_delimited_term_freq"]
  21. }
  22. }
  23. }
  24. },
  25. "mappings": {
  26. "properties": {
  27. "f1": {
  28. "type": "keyword"
  29. },
  30. "f2": {
  31. "type": "text",
  32. "analyzer": "custom_delimited_analyzer",
  33. "index_options": "freqs"
  34. }
  35. }
  36. }
  37. }

copy

The test index uses a keyword tokenizer, a delimited term frequency token filter (where the delimiter is ^), and a custom analyzer that includes a keyword tokenizer and a delimited term frequency token filter. The mappings specify that the field f1 is a keyword field and the field f2 is a text field. The field f2 uses the custom analyzer defined in the settings for text analysis. Additionally, specifying index_options signals to OpenSearch to add the term frequencies to the inverted index. You’ll use the term frequencies to give documents with repeated terms a higher score.

Next, index two documents using bulk upload:

  1. POST /_bulk?refresh=true
  2. {"index": {"_index": "test", "_id": "doc1"}}
  3. {"f1": "v0|100", "f2": "v1^30"}
  4. {"index": {"_index": "test", "_id": "doc2"}}
  5. {"f2": "v2|100"}

copy

The following query searches for all documents in the index and calculates document scores as the term frequency of the term v1 in the field f2:

  1. GET /test/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "match_all": {}
  7. },
  8. "script_score": {
  9. "script": {
  10. "source": "termFreq(params.field, params.term)",
  11. "params": {
  12. "field": "f2",
  13. "term": "v1"
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

copy

In the response, document 1 has a score of 30 because the term frequency of the term v1 in the field f2 is 30. Document 2 has a score of 0 because the term v1 does not appear in f2:

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 30,
  16. "hits": [
  17. {
  18. "_index": "test",
  19. "_id": "doc1",
  20. "_score": 30,
  21. "_source": {
  22. "f1": "v0|100",
  23. "f2": "v1^30"
  24. }
  25. },
  26. {
  27. "_index": "test",
  28. "_id": "doc2",
  29. "_score": 0,
  30. "_source": {
  31. "f2": "v2|100"
  32. }
  33. }
  34. ]
  35. }
  36. }

Parameters

The following table lists all parameters that the delimited_term_freq supports.

ParameterRequired/OptionalDescription
delimiterOptionalThe delimiter used to separate tokens from term frequencies. Must be a single non-null character. Default is |.