简体   繁体   English

具有嵌套字段的Elasticsearch搜索查询

[英]Elasticsearch search query with nested fields

I am working on a resume database on elasticsearch. 我正在使用elasticsearch上的简历数据库。 there are nested fields. 有嵌套的字段。 For example, there is a "skills" section. 例如,有一个“技能”部分。 "skills" is a nested field containing "skill" and "years". “技能”是包含“技能”和“年”的嵌套字段。 I want to be able to do a query that returns a skill with a certain year. 我希望能够执行一个返回特定年份技能的查询。 For example, I want to get resumes of people with 3 or more years of "python" experience. 例如,我想获得具有3年或更长时间“蟒蛇”经验的人的简历。

I have successfully run a query that does the following: 我已成功运行执行以下操作的查询:

It returns all the resumes that has "python as a skills.skill and 3 as a skills.year This returns result where python is associated with 2 years or experience as long as some other field is associated with 3 years of experience. 它返回所有简历,其中“python作为技能。技能,3作为技能。年份返回结果,其中python与2年或经验相关,只要其他一些领域与3年的经验相关联。

GET /resumes/_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "skills.skill":   "python" }}, 
        { "match": { "skills.years": 3 }}  
      ]
    }
  }
}

Is there a better way to sort the data where that 3 is more associated with python? 有没有更好的方法来排序数据,其中3与python更相关?

You need to make use of Nested DataType and corresponding to it you would need to make use of Nested Query 您需要使用嵌套数据类型并与之对应,您需要使用嵌套查询

What you have in current model appears to be basic object model . 您在当前模型中拥有的内容似乎是基本对象模型

I've mentioned sample mapping, sample documents, nested query and response below. 我在下面提到了示例映射,示例文档,嵌套查询和响应。 This would give you what you are looking for. 这会给你你想要的东西。

Mapping 制图

PUT resumes
{
  "mappings": {
    "mydocs": {
      "properties": {
        "skills": {
          "type": "nested",
          "properties": {
            "skill": {
              "type": "keyword"
            },
            "years": {
              "type": "integer"
            }

          }
        }
      }
    }
  }
}

Sample Documents: 样本文件:

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
    }
    ]
}

Query 询问

POST resumes/_search
{
  "query": {
    "nested": {
      "path": "skills",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "skills.skill": "python"
              }
            },
            {
              "match": {
                "skills.years": 3
              }
            }
          ]
        }
      }
    }
  }
}

Query Response: 查询响应:

{
  "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
            }
          ]
        }
      }
    ]
  }
}

Note that you only retrieve the document having id 1 in the above response. 请注意,您只检索上述响应中ID为1的文档。 Also note that just for sake of simplicity I've made skills.skill as keyword type. 另请注意,为了简单起见,我将技巧.skill作为keyword类型。 You can change it to text depending on your use case. 您可以根据用例将其更改为text

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM