从下面开始,候选人数据结构如何编写查询以获取查询候选人的记录,必须具有Java经验> 1和SQL经验> 2,并且 Angular 经验> 1很好,但不是必须的。下面的数据结构是否可能,如果没有,该如何构造该数据
//Cand - 1
{
"canId": 1,
"skill": "Java",
"yearsOfExp": 2
},
{
"canId":1,
"skill": "SQL",
"yearsOfExp": 1
},
{
"canId": 1,
"skill": "Angular",
"yearsOfExp": 1
},
{
"canId": 1,
"skill": "AngularJS",
"yearsOfExp": 1
}
//Cand - 2
{
"canId": 2,
"skill": "Jr.Software Developer",
"yearsOfExp": 3
},
{
"canId":2,
"skill": "SQL",
"yearsOfExp": 2
},
{
"canId": 2,
"skill": "Angular",
"yearsOfExp": 2
},
{
"canId": 2,
"skill": "AngularJS",
"yearsOfExp": 5
}
最佳答案
how to structure that data
修改了数据的结构,并为数据建立了索引(以嵌套文档的形式)。 nested type是对象数据类型的专用版本,它允许以可以彼此独立地查询它们的方式对对象数组进行索引。
添加包含索引数据,映射,搜索查询和搜索结果的工作示例。
索引映射:
{
"mappings": {
"properties": {
"data": {
"type": "nested"
}
}
}
}
索引数据:{
"canId": 1,
"data": [
{
"skill": "Java",
"yearsOfExp": 2
},
{
"skill": "SQL",
"yearsOfExp": 3
},
{
"skill": "Angular",
"yearsOfExp": 2
},
{
"skill": "AngularJS",
"yearsOfExp": 1
}
]
}
搜索查询:{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "data",
"query": {
"bool": {
"must": [
{
"match": {
"data.skill": "Java"
}
}
],
"filter": {
"script": {
"script": {
"source": "doc['data.yearsOfExp'].value > 1",
"lang": "painless"
}
}
}
}
}
}
},
{
"nested": {
"path": "data",
"query": {
"bool": {
"must": [
{
"match": {
"data.skill": "SQL"
}
}
],
"filter": {
"script": {
"script": {
"source": "doc['data.yearsOfExp'].value > 2",
"lang": "painless"
}
}
}
}
}
}
}
],
"should": {
"nested": {
"path": "data",
"query": {
"bool": {
"must": [
{
"match": {
"data.skill": "Angular"
}
}
],
"filter": {
"script": {
"script": {
"source": "doc['data.yearsOfExp'].value > 1",
"lang": "painless"
}
}
}
}
}
}
}
}
}
}
搜索结果:"hits": [
{
"_index": "stof_64339149",
"_type": "_doc",
"_id": "1",
"_score": 3.6119184,
"_source": {
"canId": 1,
"data": [
{
"skill": "Java",
"yearsOfExp": 2
},
{
"skill": "SQL",
"yearsOfExp": 3
},
{
"skill": "Angular",
"yearsOfExp": 2
},
{
"skill": "AngularJS",
"yearsOfExp": 1
}
]
}
}
]
关于elasticsearch - 弹性查询相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64339149/