Reverse token filter

Reverse token filter

Reverses each token in a stream. For example, you can use the reverse filter to change cat to tac.

Reversed tokens are useful for suffix-based searches, such as finding words that end in -ion or searching file names by their extension.

This filter uses Lucene’s ReverseStringFilter.

Example

The following analyze API request uses the reverse filter to reverse each token in quick fox jumps:

  1. resp = client.indices.analyze(
  2. tokenizer="standard",
  3. filter=[
  4. "reverse"
  5. ],
  6. text="quick fox jumps",
  7. )
  8. print(resp)
  1. response = client.indices.analyze(
  2. body: {
  3. tokenizer: 'standard',
  4. filter: [
  5. 'reverse'
  6. ],
  7. text: 'quick fox jumps'
  8. }
  9. )
  10. puts response
  1. const response = await client.indices.analyze({
  2. tokenizer: "standard",
  3. filter: ["reverse"],
  4. text: "quick fox jumps",
  5. });
  6. console.log(response);
  1. GET _analyze
  2. {
  3. "tokenizer" : "standard",
  4. "filter" : ["reverse"],
  5. "text" : "quick fox jumps"
  6. }

The filter produces the following tokens:

  1. [ kciuq, xof, spmuj ]

Add to an analyzer

The following create index API request uses the reverse filter to configure a new custom analyzer.

  1. resp = client.indices.create(
  2. index="reverse_example",
  3. settings={
  4. "analysis": {
  5. "analyzer": {
  6. "whitespace_reverse": {
  7. "tokenizer": "whitespace",
  8. "filter": [
  9. "reverse"
  10. ]
  11. }
  12. }
  13. }
  14. },
  15. )
  16. print(resp)
  1. response = client.indices.create(
  2. index: 'reverse_example',
  3. body: {
  4. settings: {
  5. analysis: {
  6. analyzer: {
  7. whitespace_reverse: {
  8. tokenizer: 'whitespace',
  9. filter: [
  10. 'reverse'
  11. ]
  12. }
  13. }
  14. }
  15. }
  16. }
  17. )
  18. puts response
  1. const response = await client.indices.create({
  2. index: "reverse_example",
  3. settings: {
  4. analysis: {
  5. analyzer: {
  6. whitespace_reverse: {
  7. tokenizer: "whitespace",
  8. filter: ["reverse"],
  9. },
  10. },
  11. },
  12. },
  13. });
  14. console.log(response);
  1. PUT reverse_example
  2. {
  3. "settings" : {
  4. "analysis" : {
  5. "analyzer" : {
  6. "whitespace_reverse" : {
  7. "tokenizer" : "whitespace",
  8. "filter" : ["reverse"]
  9. }
  10. }
  11. }
  12. }
  13. }