我使用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_defined字段,键的数目不固定,值的类型都是字符串,希望每个键都能被搜索,如何定义映射?如何用Elasticsearch搜索这类数据?
有人有好主意吗?
发布于 2016-03-21 04:55:44
您可以将user_defined的映射定义为"type": "object",如下所示:
PUT your_index
{
"mappings": {
"your_type": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"user_defined": {
"type": "object"
}
}
}
}
}之后,您可以对文档进行索引,并使用查询DSL轻松地搜索它们。
POST your_index/_search
{
"query": {
"match" : {
"user_defined.a" : "aaa"
}
}
}发布于 2016-03-21 03:11:14
默认情况下,Elasticsearch中的文档中的每个字段都是可索引和可搜索的。
Elasticsearch提供了一个搜索Lite API和一个用于搜索的查询DSL。
https://stackoverflow.com/questions/36122516
复制相似问题