[英]Search for documents in elasticsearch and then query the nested fields
[英]Elasticsearch search query with nested fields
我正在使用elasticsearch上的簡歷數據庫。 有嵌套的字段。 例如,有一個“技能”部分。 “技能”是包含“技能”和“年”的嵌套字段。 我希望能夠執行一個返回特定年份技能的查詢。 例如,我想獲得具有3年或更長時間“蟒蛇”經驗的人的簡歷。
我已成功運行執行以下操作的查詢:
它返回所有簡歷,其中“python作為技能。技能,3作為技能。年份返回結果,其中python與2年或經驗相關,只要其他一些領域與3年的經驗相關聯。
GET /resumes/_search
{
"query": {
"bool": {
"must": [
{ "match": { "skills.skill": "python" }},
{ "match": { "skills.years": 3 }}
]
}
}
}
有沒有更好的方法來排序數據,其中3與python更相關?
您在當前模型中擁有的內容似乎是基本對象模型 。
我在下面提到了示例映射,示例文檔,嵌套查詢和響應。 這會給你你想要的東西。
PUT resumes
{
"mappings": {
"mydocs": {
"properties": {
"skills": {
"type": "nested",
"properties": {
"skill": {
"type": "keyword"
},
"years": {
"type": "integer"
}
}
}
}
}
}
}
POST resumes/mydocs/1
{
"skills": [
{
"skill": "python",
"years": 3
},
{
"skill": "java",
"years": 3
}
]
}
POST resumes/mydocs/2
{
"skills": [
{
"skill": "python",
"years": 2
},
{
"skill": "java",
"years": 3
}
]
}
POST resumes/_search
{
"query": {
"nested": {
"path": "skills",
"query": {
"bool": {
"must": [
{
"match": {
"skills.skill": "python"
}
},
{
"match": {
"skills.years": 3
}
}
]
}
}
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.6931472,
"hits": [
{
"_index": "resumes",
"_type": "mydocs",
"_id": "1",
"_score": 1.6931472,
"_source": {
"skills": [
{
"skill": "python",
"years": 3
},
{
"skill": "java",
"years": 3
}
]
}
}
]
}
}
請注意,您只檢索上述響應中ID為1
的文檔。 另請注意,為了簡單起見,我將技巧.skill作為keyword
類型。 您可以根據用例將其更改為text
。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.