elasticsearch/java中日期的动态Map和嵌套排序

smdncfj3  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(318)

我对ElasticSearch还不熟悉。我正在使用java、ElasticSearch和spring。我的场景是在页面上有一个jquery表。此表是所有用户的列表。与用户关联的是 Set 我们称之为 extraDataValues . 这可以是任何东西,最重要的是存储的值是 String 值,可以是格式化的 Date 或者 DateTime 字符串值。这里是json:

"extraDataValues": [
    {
        "id": 3,
        "extraDataValueObject": {
            "id": 12,
            "label": "Metadata Date",
            "displayable": true
        },
        "value": "01/01/2015 00:01:11"
    },
    {
        "id": 4,
        "extraDataValueObject": {
            "id": 13,
            "label": "Metadata TextBox",
            "displayable": true
        },
        "value": "zzzz"
    }
],

这让我想到了这个问题。考虑到我有一个嵌套的 extraDataValues 在用户中设置 extraDataObjects ,如何:
对驻留在extradatavalues中的id为12或13的特定extradataobject执行排序。基本上,在java中 SetextraDataValues 在那里我会寻找所有 extraDataValueObjects 如果id为12,我将查看这是否是日期时间,并将其排序为日期时间
如果是日期或日期时间,则对字符串值执行排序?
如何在运行时动态更改Map,以便执行这种动态排序
如何在纯ElasticSearch中做到这一点,如何将其转换为java?
我可以计算出这个值是否是 DateTime 或者java中的“字符串”,但我不知道如何将其转换为java中的ElasticSearch。
简单查询后ElasticSearch返回的用户对象:

User:
{
    "id": 1,
     "extraDataValues": [
        {
            "id": 1,
            "extraDataValueObject": {
                "id": 10,
                "label": "Metadata Date",
                "displayable": true
            },
            "value": "01/01/2015 00:00:00"
        },
        {
            "id": 2,
            "extraDataValueObject": {
                "id": 11,
                "label": "Metadata TextBox",
                "displayable": true
            },
            "value": "aaaa"
        }
    ],
    "username": "johnDoe",
    "firstName": "John",
    "surname": "Doe",
    "email": "john@doe.com",
    "fullName": "John Doe"

    "type": {
        "id": 2,
        "name": "Blogger",
        "active": true,
    },
    "club": {
        "id": 2,
        "name": "Photography",
    }
},
{
    "id": 2,
     "extraDataValues": [
        {
            "id": 3,
            "extraDataValueObject": {
                "id": 10,
                "label": "Metadata Date",
                "displayable": true
            },
            "value": "01/01/2015 00:01:11"
        },
        {
            "id": 4,
            "extraDataValueObject": {
                "id": 11,
                "label": "Metadata TextBox",
                "displayable": true
            },
            "value": "zzzz"
        }
    ],
    "username": "marySmith",
    "firstName": "Mary",
    "surname": "Mary",
    "email": "mary@smith.com",
    "fullName": "Mary Smith"

    "type": {
        "id": 2,
        "name": "Moderator",
        "active": true,
    },
    "club": {
        "id": 2,
        "name": "Yoga",
    }
},
{
    "id": 3,
    "extraDataValues": [
        {
            "id": 5,
            "extraDataValueObject": {
                "id": 10,
                "label": "Metadata Date",
                "displayable": true
            },
            "value": "02/02/2015 00:01:11"
        },
        {
            "id": 6,
            "extraDataValueObject": {
                "id": 11,
                "label": "Metadata TextBox",
                "displayable": true
            },
            "value": "bbbb"
        }
    ],
    "username": "joeBloggs",
    "firstName": "Joe",
    "surname": "Bloggs",
    "email": "joe@bloggs.com",
    "fullName": "Joe Bloggs"

    "type": {
        "id": 3,
        "name": "Admin",
        "active": true,
    },
    "club": {
        "id": 3,
        "name": "Cycling",
    }
}

用户Map文档:

{
    "User": {
       "properties": {
            "type": {
                "properties": {
                    "id": {
                        "type": "long"
                    },
                    "name": {
                        "type": "string",
                        "index":    "not_analyzed",
                        "fields": {
                            "raw_lower_case": { 
                                "type":  "string",
                                "analyzer": "case_insensitive"
                            }
                        }
                    }
                }
            },
            "fullName": {
                "type": "string",
                "index":    "not_analyzed",
                "fields": {
                    "raw_lower_case": { 
                        "type":  "string",
                        "analyzer": "case_insensitive"
                    }
                }
            },              
            "username": {
                "type": "string",
                "index":    "not_analyzed",
                "fields": {
                    "raw_lower_case": { 
                        "type":  "string",
                        "analyzer": "case_insensitive"
                    }
                }
            },
            "email": {
                "type": "string",
                "index":    "not_analyzed",
                "fields": {
                    "raw_lower_case": { 
                        "type":  "string",
                        "analyzer": "case_insensitive"
                    }
                }
            },
            "firstName": {
                "type": "string",
                "index":    "not_analyzed",
                "fields": {
                    "raw_lower_case": { 
                        "type":  "string",
                        "analyzer": "case_insensitive"
                    }
                }
            },
            "surname": {
                "type": "string",
                "index":    "not_analyzed",
                "fields": {
                    "raw_lower_case": { 
                        "type":  "string",
                        "analyzer": "case_insensitive"
                    }
                }
            },
            "id": {
                "type": "long"
            },
            "metadataFieldValues": {
                "type": "nested",
                "properties": {
                    "metadataFieldDefinition": {
                        "properties": {
                            "id": {
                                "type": "long"
                            },
                            "label": {
                                "type": "string"
                            },
                            "displayable": {
                                "type": "boolean"
                            }
                        }
                    },
                    "value": {
                        "type": "string",
                        "index":    "not_analyzed",
                        "fields": {
                            "raw_lower_case": { 
                                "type":  "string",
                                "analyzer": "case_insensitive"
                            }
                        }
                    }
                }
            },
            "club": {
                "properties": {
                    "id": {
                        "type": "long"
                    },
                    "name": {
                        "type": "string",
                        "index":    "not_analyzed",
                        "fields": {
                            "raw_lower_case": { 
                                "type":  "string",
                                "analyzer": "case_insensitive"
                            }
                        }
                    }
                }
            },      
        }
    }
}

编辑:::
我看了一下这些问题:-elasticseach-按日期排序-elasticsearch按数组中的单个嵌套文档键排序
我的方向对吗?

lqfhib0f

lqfhib0f1#

属性在elasticsearch中的所有文档中必须具有相同的类型/Map。以后不能更改。
一旦你索引 extraDataValues.value 作为一个字符串,它仍然是一个字符串,并且在所有文档中必须是一个字符串。
也就是说,也许你可以看看基于脚本的排序。但是,即使在这种情况下,似乎也无法生成新类型的脚本字段。
您可能需要考虑另一种建模方法—在附加字段中将日期索引为日期类型。
顺便说一下,还有其他引擎,比如siren,它可以让您在不同的文档中为相同的属性使用不同的类型。免责声明:我目前在一家开发塞壬的公司工作。

相关问题