gpt4 book ai didi

elasticsearch - Elasticsearch Copy_to数据需要复制自己的子文档

转载 作者:行者123 更新时间:2023-12-03 01:30:12 25 4
gpt4 key购买 nike

在此先感谢您的帮助。

我已将ES映射创建为:

{"mappings": {
"policy": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"properties": {
"scope": {
"type": "text",
"store": "true",
"copy_to": [
"tags.tag_scope"
]
},
"tag": {
"type": "text",
"store": "true",
"copy_to": [
"tags.tag_scope"
]
},
"tag_scope": {
"type": "text",
"store": "true"
}
}
}
}
}
}

}

当我索引策略文档时,来自不同标签文档的所有标签和作用域值都将复制到tag_scope属性。

例如,我添加了一个关于 flex 搜索的文档:
{
"name": "policy1",
"tags": [
{
"tag": "pepsi",
"scope": "prod"
},
{
"tag": "coke",
"scope": "dev"
}
]
}

它将所有4个值存储在tag_scope文档中,如下所示:

“tags.tag_scope”:[
“百事可乐”,
“测试”,
“可乐”,
“dev”
]

我的异常(exception)是,它应该像这样存储:
 {
"name": "policy1",
"tags": [
{
"tag": "pepsi",
"scope": "prod",
"tag_scope" : ["pepsi","prod"]
},
{
"tag": "coke",
"scope": "dev",
"tag_scope" : ["coke","dev"]
}
]
}

您能帮我做同样的正确映射吗?

最佳答案

您正在寻找的是 Nested Datatype 。将您的映射更改为以下内容:

PUT <your_index_name>
{
"mappings":{
"policy":{
"properties":{
"name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"tags":{
"type": "nested",
"properties":{
"scope":{
"type":"text",
"store":"true",
"copy_to":[
"tags.tag_scope"
]
},
"tag":{
"type":"text",
"store":"true",
"copy_to":[
"tags.tag_scope"
]
},
"tag_scope":{
"type":"text",
"store":"true",
"fields": { <---- Added this
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
}
}

注意我如何将 tags设置为 nested 类型。这样可以将以下内容存储为单独的文档本身,在您的情况下 tags基本上具有两个嵌套的文档。
{  
"tag":"coke",
"scope":"dev"
}

现在,您的 tags.tag_scope应该就是您所期望的。

现在,当查询您要查找的内容时,下面是 Nested Query的样子。

嵌套查询:
POST <your_index_name>/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{
"match": {
"tags.tag_scope": "pepsi"
}
},
{
"match": {
"tags.tag_scope": "prod"
}
}
]
}
}
}
}
]
}
}
}

要返回唯一 tags.tag_scope值的列表,您将需要返回聚合查询。注意,我已经提到了 size:0,这意味着我只想查看聚合结果,而不是正常的查询结果。

汇总查询:
POST <your_index_name>/_search
{
"size":0,
"query":{
"bool":{
"must":[
{
"nested":{
"path":"tags",
"query":{
"bool":{
"must":[
{
"match":{
"tags.tag_scope":"pepsi"
}
},
{
"match":{
"tags.tag_scope":"prod"
}
}
]
}
}
}
}
]
}
},
"aggs":{ <----- Aggregation Query Starts Here
"myscope":{
"nested":{
"path":"tags"
},
"aggs":{
"uniqui_scope":{
"terms":{
"field":"tags.tag_scope.keyword",
"size":10
}
}
}
}
}
}

聚集响应:
{
"took": 53,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"myscope": {
"doc_count": 2,
"uniqui_scope": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "coke",
"doc_count": 1
},
{
"key": "dev",
"doc_count": 1
},
{
"key": "pepsi",
"doc_count": 1
},
{
"key": "prod",
"doc_count": 1
}
]
}
}
}
}

希望这可以帮助。

关于elasticsearch - Elasticsearch Copy_to数据需要复制自己的子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com