How to configure multiple analyzers for one field

2019-06-13 17:27发布

问题:

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:

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.