gpt4 book ai didi

mysql - Elasticsearch:如何搜索用户定义的字段?

转载 作者:行者123 更新时间:2023-11-29 21:13:54 25 4
gpt4 key购买 nike

我使用mysql存储用户数据,并通过Elasticsearch搜索数据。对于mysql,我有一个用户定义的字段,该字段可以存储JSON格式的数据。

示例数据如下:

data1 = 
{
"name" : "test1",
"age": 10,
"user_defined": {
"a" : "aaa",
"b" : "bbb",
"c" : "ccc",
.....
}
}

data2 =
{
"name" : "test2",
"age": 20,
"user_defined": {
"d" : "ddd",
"e" : "eee",
"f" : "fff",
.....
}
}

对于user_define字段,key的个数不固定,value的类型都是string,希望每个key都能被查找到,如何定义映射?如何通过Elasticsearch搜索此类数据?

谁有好主意吗?

最佳答案

您可以将user_define的映射定义为"type": "object" ,像这样:

PUT your_index
{
"mappings": {
"your_type": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"user_defined": {
"type": "object"
}
}
}
}
}

此后,您可以使用Query DSL索引您的文档并轻松搜索它们。

POST your_index/_search
{
"query": {
"match" : {
"user_defined.a" : "aaa"
}
}
}

关于mysql - Elasticsearch:如何搜索用户定义的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122516/

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