Elasticsearch - 使用路径层次标记器访问不同级别的类别 [英] Elasticsearch - using the path hierarchy tokenizer to access different level of categories

查看:19
本文介绍了Elasticsearch - 使用路径层次标记器访问不同级别的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Elasticsearch 的新手,对路径的分层标记器有疑问.这是我的代码示例:

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

我的映射代码:

PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "path-analyzer": {
          "type": "custom",
          "tokenizer": "path-tokenizer"
        }
      },
      "tokenizer": {
        "path-tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "."
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "dynamic": "strict",
      "properties": {
        "group_path": {
          "type": "string",
          "index_analyzer": "path-analyzer",
          "search_analyzer": "keyword"
        }
      }
    }
  }
}

这是我的 PUT:

PUT /my_index/my_type/1
{
  "group_path": ["Book.Thriller.Adult","DVD.Comedy.Kids"]
}

这是我的查询:

GET /my_index/my_type/_search?search_type=count
{
   "aggs": {
      "category": {
         "terms": {
            "field": "group_path",
            "size": 0
         }
      }
   }
}

结果:

{
   ...
   "aggregations": {
      "category": {
         "buckets": [
            {
               "key": "Book",
               "doc_count": 1
            },
            {
               "key": "Book.Thriller",
               "doc_count": 1
            },
            {
               "key": "Book.Thriller.Adult",
               "doc_count": 1
            },
            {
               "key": "DVD",
               "doc_count": 1
            },
            {
               "key": "DVD.Comedy",
               "doc_count": 1
            },
            {
               "key": "DVD.Comedy.Kids",
               "doc_count": 1
            }
         ]
      }
   }
}

到目前为止一切都很好.我正在寻找的是如何为第一类创建存储桶.我怎样才能得到这样的结果:

So far is everything good. What I'm looking for is that how can I create buckets for example only for the first category. How can I get result like that:

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

感谢您的帮助.

推荐答案

我发现这样做的唯一方法是使用排除语法来排除您不想要的级别.

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" : ".*\..*"
         }
      }
   }
}

然后会回来

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": ".*\..*\..*"
        }
      }
    }
}

然后会回来

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

这篇关于Elasticsearch - 使用路径层次标记器访问不同级别的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆