在收集时使用术语筛选查询



我正在维护一个旧项目,并且从一天开始就被困在查询上。
我使用的 elasticsearch 版本是 1.7,但我认为这与我的问题无关。

我有一些教师文件:

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

教师类纪律关系是这种格式的 N 倍(对于我拥有的每个几个级别树/学科)

{
  "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。知道为什么吗?

因为teacherClassDisciplineRelation是一个嵌套字段。您必须使用嵌套查询。

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

希望这有帮助!!

最新更新