1

I am trying to add path hierarchy analyzer to one field in my type.

I want to configure path analyzer and reverse path analyzer on same field so I can get normal path and reverse path of the one field.

for eg.

path="/angular/directive/structural"

I want to configure this path field such as whenever I analyze this field, I should get tokens in normal order and reverse order if I specify reverse.

Below is my mapping.

PUT /elastic_course
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path_analyzer": {
          "tokenizer": "path_tokenizer"
        },
        "reverse_path_analyzer": {
          "tokenizer": "reverse_path_tokenizer"
        }
      },
      "tokenizer": {
        "path_tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "/",
          "replacement": "-"
        },
        "reverse_path_tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "/",
          "replacement": "-"
        }
      }
    }
  },
  "mappings": {
    "book" : {
        "properties": {
           "author": {
              "type": "string",
              "index": "not_analyzed"
           },
           "genre": {
              "type": "string",
              "index": "not_analyzed"
           },
           "score": {
              "type": "double"
           },
           "synopsis": {
              "type": "string",
              "index":"analyzed",
              "analyzer":"english"
           },
           "title": {
              "type": "string"
           },
           "path":{
              "type":"string",
              "analyzer":"path_analyzer",
              "fields": {
                "reverse": {
                  "type":"string",
                  "analyzer":"reverse_path_analyzer"
                }
              }
          }
      }
    }
  }
}

I have configured this analyzers. Now How to I get tokens for path and reverse path?

Thanks.

1 Answer 1

5

You can declare sub-fields for the path field, each with a different analyzer:

       "path":{
          "type":"string",
          "analyzer":"path_analyzer",
          "fields": {
            "reverse": {
              "type":"string",
              "analyzer":"reverse_path_analyzer"
            }
          }
       }

Then you can refer to path and path.reverse in your queries.

Sign up to request clarification or add additional context in comments.

4 Comments

Of course you need to declare the proper analyzers in your index settings.
Cool, looks good. At indexing time, the path and path.reverse fields will be analyzed for you by ES.
sorry, but I am not understanding. I have inserted one document in my index now. It has path field as /book/angular/structural. what query I should run in my sense plugin to see the tokens ?
You can use the Analyze API to see how the path is tokenized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.