弹性查询相关数据

问题描述

从下面开始,候选者数据结构如何编写查询获取查询候选者的记录必须具有Java经验> 1和sql经验> 2,并且具有角度经验> 1很好,但不是必须的。下面的数据结构是否可行?如果没有,如何构造数据

//Cand - 1
 { 
   "canId": 1,"skill": "Java","yearsOfExp": 2
 },{
   "canId":1,"skill": "sql","yearsOfExp": 1
 },{
   "canId": 1,"skill": "Angular","skill": "AngularJS","yearsOfExp": 1
 }
 
 //Cand - 2
 { 
   "canId": 2,"skill": "Jr.software Developer","yearsOfExp": 3
 },{
   "canId":2,{
   "canId": 2,"yearsOfExp": 5
 }

解决方法

如何构造数据

已经修改了数据的结构,并为数据建立了索引(以嵌套文档的形式)。 nested type是对象数据类型的专门版本,它允许以可以彼此独立地查询它们的方式来索引对象数组。

添加包含索引数据,映射,搜索查询和搜索结果的工作示例。

索引映射:

{
  "mappings": {
    "properties": {
      "data": {
        "type": "nested"
      }
    }
  }
}

索引数据:

{
  "canId": 1,"data": [
    {
      "skill": "Java","yearsOfExp": 2
    },{
      "skill": "SQL","yearsOfExp": 3
    },{
      "skill": "Angular",{
      "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",{
              "skill": "AngularJS","yearsOfExp": 1
            }
          ]
        }
      }
    ]