Elasticsearch - using the path hierarchy tokenizer to access different level of categories

前端 未结 1 1753
小蘑菇
小蘑菇 2020-12-23 18:15

I\'m very new in Elasticsearch and have a question about the hierarchical tokenizer of a path. Here is my code example:

My mapping code:

PUT /my_inde         


        
相关标签:
1条回答
  • 2020-12-23 18:37

    The only way I found to do this is to use the exclude syntax to exclude the levels you don't want.

        {
       "aggs": {
          "category": {
             "terms": {
                "field": "group_path",
                "size": 0, 
                "exclude" : ".*\\..*"
             }
          }
       }
    }
    

    Will then return

    aggregations: {
         category: {
           buckets: [
              {
                 key: Book
                 doc_count: 1
              }
              {
                 key: DVD
                doc_count: 1
              }
           ]
         }
    }
    

    If you select book, you can then search like this

    {
        "query" : {
            "filtered": {
                "filter": {
            "prefix": {
              "group_path": "Book"
            }
                }
            }
        },
        "aggs" : {
          "category": {
            "terms": {
              "field": "group_path",
              "size": 0,
              "include" : "Book\\..*",
              "exclude": ".*\\..*\\..*"
            }
          }
        }
    }
    

    Will then return

    aggregations: {
         category: {
           buckets: [
              {
                 key: Book.Thriller
                 doc_count: 1
              }
           ]
         }
    }
    
    0 讨论(0)
提交回复
热议问题