我是 Elastic Search 新手。我正在使用 Java、Elastic Search 和 Spring。我的场景是页面上有一个 jQuery 表。该表是所有用户的列表。与用户关联的是一组额外信息,我们将其称为“extraDataValues”。这可以是任何东西,最重要的是存储的值是一个 String
值,它可以是格式化的 Date
或 DateTime
String 值。这是 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"
}
],
这让我想到了这个问题。鉴于我在具有 extraDataObjects
的用户中设置了嵌套的 extraDataValues
,我该如何:
- 对位于 extraDataValues 中 ID 为 12 或 13 的特定 extraDataObject 执行排序。基本上,在 Java 中,我将获取
extraDataValues
的Set
,并在其中搜索 ID 为 12 的所有extraDataValueObjects
,然后查看这是否是日期时间并将其排序为日期时间 - 如果字符串值是日期或日期时间,则对其执行排序?
- 如何在运行时动态更改映射,以便执行此类动态排序
- 如何在普通 Elastic Search 中执行此操作以及如何将其转换为 Java?
我可以判断该值是 Java 中的 DateTime 还是“字符串”,但我不知道如何将其转换为 Java 中的 Elastic Search。
简单查询后从 Elastic Search 返回用户对象:
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",
}
}
用户映射文档:
{
"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 - Sorting on dates -Elasticsearch sort by single nested document key in array
我的方向正确吗?
最佳答案
Elasticsearch 中的所有文档中的属性必须具有相同的类型/映射。您以后无法更改它。
一旦将 extraDataValues.value
索引为字符串,它将仍然是字符串,并且在所有文档中都必须是字符串。
话虽这么说,也许你可以看看 script-based sorting 。但是,即使在这种情况下,您似乎也无法生成新类型的脚本字段。
您可能需要考虑不同的建模 - 将日期索引为附加字段中的日期类型。
对了,还有其他引擎,比如SIREn这让您可以在不同的文档中为相同的属性拥有不同的类型。 免责声明:我目前在一家开发 SIREn 的公司工作。
关于java - ElasticSearch/Java 中日期的动态映射和嵌套排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29916207/