【问题标题】:how to write a must query with nested mappings along with other conditions如何使用嵌套映射以及其他条件编写必须查询
【发布时间】:2021-06-13 11:40:37
【问题描述】:

如何在技能列上获取条件为“(角度或角度 js)和(java 或核心 java)和 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"
 } 

【问题讨论】:

  • 你能添加一个应该在结果中返回的示例文档
  • @jaspreetchahal 添加了示例文档

标签: elasticsearch search nested


【解决方案1】:

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

嵌套查询将返回满足其中所有子句的嵌套文档。 因此,您的查询正在尝试查找具有技能= 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"
        }
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多