【发布时间】:2018-03-29 03:45:57
【问题描述】:
我是 ElasticSearch 的新手,我有一棵树,它描述了某个文档的路径(不是真正的文件系统路径,只是将文章、图像、文档分类为一个的简单文本字段)。每个路径条目都有一个类型,例如:Group Name、Assembly name 甚至 Unknown。例如,可以在查询中使用这些类型来跳过路径中的某些条目。
通过将Tree.Id 连接到Tree.ParentId 来构建树,但每个节点都必须有一个类型。文档与树中的叶子相连。
我不担心在 SQL Server 中查询结构,但是我应该找到一种最佳方法来非规范化并在 Elastic 中搜索它们。如果我将路径展平并为文档制作“描述符”列表,我可以将每个文档条目存储为弹性文档。:
{
"path": "NodeNameRoot/NodeNameLevel_1/NodeNameLevel_2/NodeNameLevel_3/NodeNameLevel_4",
"descriptors": [
{
"name": "NodeNameRoot",
"type": "type1"
},
{
"name": "NodeNameLevel_1",
"type": "type1"
},
{
"name": "NodeNameLevel_2",
"type": "type2"
},
{
"name": "NodeNameLevel_3",
"type": "type2"
},
{
"name": "NodeNameLevel_4",
"type": "type3"
}
],
"document": {
...
}
}
我可以在 ElasticSearch 中查询这样的结构吗?或者我应该以不同的方式对路径进行非规范化吗?
我的主要问题:
可以根据类型或文本值查询它们(例如正则表达式匹配)。例如:给我所有的 type2->type3 路径(实际上将 type1 排除在外),其中路径包含X?
可以根据等级查询吗?就像我想要有 4 个描述符的路径。
我可以使用内置功能进行搜索还是需要编写扩展程序?
编辑 基于 G Quintana 的 anwser,我做了一个这样的索引。:
curl -X PUT \
http://localhost:9200/test \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"mappings": {
"path": {
"properties": {
"names": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"tokens": {
"type": "text",
"analyzer": "pathname_analyzer"
},
"depth": {
"type": "token_count",
"analyzer": "pathname_analyzer"
}
}
},
"types": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"tokens": {
"type": "text",
"analyzer": "pathname_analyzer"
}
}
}
}
}
},
"settings": {
"analysis": {
"analyzer": {
"pathname_analyzer": {
"type": "pattern",
"pattern": "#->>",
"lowercase": true
}
}
}
}
}'
并且可以像这样查询深度。:
curl -X POST \
http://localhost:9200/test/path/_search \
-H 'content-type: application/json' \
-d '{
"query": {
"bool": {
"should": [
{"match": { "names.depth": 5 }}
]
}
}
}'
返回正确的结果。我会再测试一下。
【问题讨论】:
标签: elasticsearch hierarchical-data denormalization