How to query fields with path_hierarchy analyzer in elasticsearch?

耗尽温柔 提交于 2021-01-28 06:06:10

问题


I have configured path_analyzer in elasticsearch using below configuration.

PUT /elastic_course
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path_analyzer": {
          "tokenizer": "path_tokenizer"
        },
        "reverse_path_analyzer": {
          "tokenizer": "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"
                }
              }
          }
      }
    }
  }
}

Now I have inserted four documents where path values are :

  • /angular/structural
  • /angular/structural/directives
  • /angular/structural/components
  • /angular/logistics

Now I want to query my index such as :

  1. it will retrieve only children of structural.
  2. It will return all the leaf nodes i.e. components , directives and logistics.

I tried running below query but it retrieves all the records.

POST elastic_course/book/_search
{
  "query": {
    "regexp": {
      "path.":"/structural"
    }
  }
}

any help?

Thanks.

来源:https://stackoverflow.com/questions/43249637/how-to-query-fields-with-path-hierarchy-analyzer-in-elasticsearch

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