如何使用嵌套映射和其他条件编写必须查询

how to write a must query with nested mappings along with other conditions

如何获取技能条件为“(angular or angular js) and (java or core java) and html”的记录具有如下所述的具有嵌套映射的集合的多年经验排序的列,以及不同列上的其他一些必须条件

 "skills" : {
      "type" : "nested",
      "properties" : {
        "skill" : {
          "type" : "keyword",
          "normalizer" : "keyword_lowercase"
        },
        "yearsOfExp" : {
          "type" : "double"
        }
      }
    }

尝试了以下查询但无法获得结果

 {
   "from": "0",
   "size": "30",
   "track_total_hits": true,
   "query": {
     "bool": {
       "must": [
         {
           "terms": {
             "status": [
               "Active"
             ]
           }
         },
         {
           "nested": {
             "path": "skills",
             "query": {
               "bool": {
                 "must": [
                   {
                     "bool": {
                       "should": [
                         {
                           "match": {
                             "skills.skill": "angular js"
                           }
                         },
                         {
                           "match": {
                             "skills.skill": "Angular"
                           }
                         }
                       ]
                     }
                   },
                   {
                     "bool": {
                       "should": [
                         {
                           "match": {
                             "skills.skill": "java"
                           }
                         },
                         {
                           "match": {
                             "skills.skill": "core java"
                           }
                         }
                       ]
                     }
                   },
                   {
                     "match": {
                       "skills.skill": "html"
                     }
                   }
                 ]
               }
             }
           }
         }
       ]
     }
   },
   "sort": [
     {
       "_score": {
         "order": "desc"
       }
     },
     {
       "totalExpInMonths": {
         "order": "desc"
       }
     }
   ]
 }

示例文档:

 {
   "id": 1295,
   "skills": [
     {
       "skill": "Test-Driven Development",
       "yearsOfExp": 0
     },
     {
       "skill": "TOAD",
       "yearsOfExp": 0
     },
     {
       "skill": "Version Control",
       "yearsOfExp": 0
     },
     {
       "skill": "WebLogic Enterprise Application Server",
       "yearsOfExp": 0
     }
   ],
   "fullName": "Abdul Shaik",
   "status": "Active"
 } 

您将需要使用多个嵌套查询。

嵌套查询将return 一个满足其中所有子句的嵌套文档。 因此,您的查询试图查找具有 skill= angular、java 和 html

的嵌套文档

正确查询:-

{
  "from": "0",
  "size": "30",
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "skills",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "skills.skill": "angular js"
                    }
                  },
                  {
                    "match": {
                      "skills.skill": "Angular"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "skills",
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "skills.skill": "java"
                    }
                  },
                  {
                    "match": {
                      "skills.skill": "core java"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "skills",
            "query": {
              "match": {
                "skills.skill": "html"
              }
            }
          }
        }
      ]
    }
  }
}

结果:

{
        "_index" : "index114",
        "_type" : "_doc",
        "_id" : "fFTzF3oB5tcHqHDtT0QO",
        "_score" : 4.081922,
        "_source" : {
          "id" : 1295,
          "skills" : [
            {
              "skill" : "angular js",
              "yearsOfExp" : 0
            },
            {
              "skill" : "java",
              "yearsOfExp" : 0
            },
            {
              "skill" : "html",
              "yearsOfExp" : 0
            }
          ],
          "fullName" : "Abdul Shaik",
          "status" : "Active"
        }
      }