带有关于集合的术语的过滤查询

时间:2017-01-16 15:29:57

标签: elasticsearch

我正在进行旧项目维护,因为查询一天后我就陷入困境 我使用的弹性搜索版本是1.7,但我不认为这与我的问题有关。

我有一些教师文件:

{
  "id": 244,
  "degree": [],
  "teacherDiplomaRelation": [],
  "user": {
    "enabled": true
  },
  "teacherClassDisciplineRelation": [
    SEE BELOW
}

teacherClassDisciplineRelation N次这种格式(对于我所拥有的每一对levelTree / Discipline)

{
  "levelTree": {
    "id": 34,
    "label": "1st year of college",
    "slugLastLevelDisplay": "college"
  },
  "discipline": {
    "id": 1,
    "label": "Maths",
    "slug": "maths"
  },
  "cityLocation": "10.1010,10.1010"
}

现在我希望所有教师都能在他们的学科中获得数学。我的疑问是:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "user.enabled": true
              }
            },
            {
              "term": {
                "teacherClassDisciplineRelation.discipline.slug": "maths"
              }
            }
          ]
        }
      }
    }
  },
  "size": {
    "from": 0,
    "size": 15
  }
}

映射:

    "teacherClassDisciplineRelation": {
        "type": "nested",
        "properties": {
          "cityLocation": {
            "type": "geo_point",
            "store": true
          },
          "discipline": {
            "properties": {
              "id": {
                "type": "string",
                "store": true
              },
              "label": {
                "type": "string",
                "boost": 7.0,
                "store": true,
                "analyzer": "custom_analyzer"
              },
              "slug": {
                "type": "string",
                "boost": 7.0,
                "index": "not_analyzed",
                "store": true,
                "norms": {
                  "enabled": true
                }
              }
            }
          }

问题:
我对"user.enabled": true的查询给了我一些结果, 我使用"teacherClassDisciplineRelation.discipline.slug": "maths" 的查询总是给我0结果但是我已经检查了索引,我应该有一些结果

我是弹性搜索的新手,但我无法找出为什么我的结果始终为0。 知道为什么吗?

1 个答案:

答案 0 :(得分:1)

由于teacherClassDisciplineRelation是嵌套字段。您必须使用Nested Query

{
"query": {
  "bool": {
     "must": [
        {
           "nested": {
              "path": "teacherClassDisciplineRelation",
              "query": {
                 "term": {
                    "teacherClassDisciplineRelation.discipline.slug": {
                       "value": "maths"
                    }
                 }
              }
           }
        },
        {
           "term": {
              "user.enabled": true
             }
          }
       ]
    }
   }
 }

希望这会有所帮助!!