How to configure multiple analyzers for one field in elasticsearch?

前提是你 提交于 2019-12-08 05:15:44

问题


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.



来源:https://stackoverflow.com/questions/43178038/how-to-configure-multiple-analyzers-for-one-field-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!