From the below, candidate Data structure How to write a query for getting records for the query candidate must have java experience > 1 and SQL experience > 2 and good to have angular experience > 1 but not must. Is it possible with the below data structure or if not how to structure that data
//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
Have modified the structure of data, and indexed the data (in the form of nested document). The nested type is a specialized version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.
Adding a working example with index data, mapping, search query, and search result.
Index Mapping:
{
"mappings": {
"properties": {
"data": {
"type": "nested"
}
}
}
}
Index Data:
{
"canId": 1,
"data": [
{
"skill": "Java",
"yearsOfExp": 2
},
{
"skill": "SQL",
"yearsOfExp": 3
},
{
"skill": "Angular",
"yearsOfExp": 2
},
{
"skill": "AngularJS",
"yearsOfExp": 1
}
]
}
Search Query:
{
"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"
}
}
}
}
}
}
}
}
}
}
Search Result:
"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
}
]
}
}
]