elasticsearch - 弹性查询相关数据

标签 elasticsearch relevance

从下面开始,候选人数据结构如何编写查询以获取查询候选人的记录,必须具有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/

相关文章:

json - 将elasticsearch.Config结构保存到JSON文件

java - Solr 函数查询以按短语 slop 排序

php - mysql按相关案例问题对结果进行排序

java - ElasticSearch 2.0 Java API 聚合过滤器与 query_string

json - Elasticsearch API从路径插入数据

elasticsearch - Elasticsearch可以建议缺少数组的项吗?

javascript - 按相关性能问题排序

elasticsearch 为同义词/词干化定制分数

elasticsearch - 如何使 Elasticsearch 评分考虑字段长度

elasticsearch - 基于字段值计数的ElasticSearch提升相关性