弹性查询返回不正确的结果

时间:2017-05-30 11:20:50

标签: elasticsearch

这是我的询问....

{
  "size": 100,
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "dkncourseintakes": [
                  "Trimester 3"
                ]
              }
            },
            {
              "range": {
                "dkncourseatarcloud": {
                  "lte": "100"
                }
              }
            },
            {
              "range": {
                "dkncourseatarwaurnponds": {
                  "lte": "100"
                }
              }
            },
            {
              "range": {
                "dkncourseatarwarrnambool": {
                  "lte": "100"
                }
              }
            },
            {
              "range": {
                "dkncourseatarburwood": {
                  "lte": "100"
                }
              }
            },
            {
              "range": {
                "dkncourseatarwaterfront": {
                  "lte": "100"
                }
              }
            }
          ],
          "must": [
            {
              "terms": {
                "dkncoursequal": [
                  "under_bachelor_degree"
                ]
              }
            }
          ]
        }
      },
      "must": [
        {
          "match": {
            "dkncoursestudent": "Domestic"
          }
        },
        {
          "terms": {
            "dknpagetagia": [
              "ia-Business"
            ]
          }
        },
        {
          "terms": {
            "dkncourselocations": [
              "Burwood"
            ]
          }
        }
      ]
    }
  }
}

这个查询返回的结果之一就是......正如你可以看到我的查询清楚地搜索" Trimester 3"中的课程,我很困惑为什么这个课程与" Trimester 1& #34;退回了?

{
        "_index": "all_courses",
        "_type": "courses",
        "_id": "03c154fe7ee8029472533222340369d43d4fbff5",
        "_score": 2.030092,
        "_source": {
          "dkncoursetype": "Undergraduate",
          "dkncoursequal": "under_bachelor_degree",
          "dkncourselocations": "Burwood",
          "dkncourseatarwarrnambool": 0,
          "dknpagetagia": "ia-Business,ia-English_and_International_Languages",
          "dkncourseatarburwood": 65,
          "dknpagetagfands": "Faculty_of_Arts_and_Education",
          "dkncourseatarwaterfront": 0,
          "dkncoursestudent": "Domestic",
          "dkncourseintakes": "Trimester 1" <=== THIS IS THE PROBLEM
        }

如果我从此

更改/调整查询
"bool": {
              "should": [
                {
                  "terms": {
                    "dkncourseintakes": [
                      "Trimester 3"
                    ]
                  }

到这个

 "bool": {
          "must": [
            {
              "terms": {
                "dkncourseintakes": [
                  "Trimester 3"
                ]
              }
            }

我没有结果。

另外,我正在使用this插件来生成请求正文。

我的查询看起来像这样..

 var body = bodybuilder()
                    //.filter('terms', 'dkncourseintakes', intake_checkbox_value)
                    .query('match', 'dkncoursestudent', student_radio_value)
                    .query('terms', 'dknpagetagia', interest_checkbox_value)
                    .query('terms', 'dkncourselocations', location_checkbox_value)
                    .filter('terms', 'dkncourseintakes', intake_checkbox_value)

                    .orFilter('range', 'dkncourseatarcloud', {lte: atar_radio_value})
                    .orFilter('range', 'dkncourseatarwaurnponds', {lte: atar_radio_value})
                    .orFilter('range', 'dkncourseatarwarrnambool', {lte: atar_radio_value})
                    .orFilter('range', 'dkncourseatarburwood', {lte: atar_radio_value})
                    .orFilter('range', 'dkncourseatarwaterfront', {lte: atar_radio_value})

                .filter('terms', 'dkncoursequal', qual_checkbox_value)
                    .size(100)
                    .build()

这是我的映射:

{
        "settings": {

            "number_of_shards": "1",
            "number_of_replicas": "0",
            "analysis": {
                "analyzer": {
                    "semi_colon_analyzer": {
                        "type": "pattern",
                        "pattern": ";",
                        "lowercase": false
                    },
                    "comma_analyzer": {
                        "type": "pattern",
                        "pattern": ",",
                        "lowercase": false
                    }
                }

            }
        },
        "mappings": {
            "test": {
                "properties": {     
                "dkncoursestudent": {
                        "type": "text"
                    },
                    "dknpagetagia": {
                        "type": "text",
                        "analyzer": "comma_analyzer",
                        "search_analyzer": "comma_analyzer"
                    },      
                    "dkncoursequal": {
                        "type": "text"
                    },
                    "dkncourseatar": {
                        "type": "integer"
                    },  
                    "dkncourseintakes": {
                        "type": "text",
                        "analyzer": "semi_colon_analyzer",
                        "search_analyzer": "semi_colon_analyzer"
                    },
                    "dkncourselocations": {
                        "type": "text",
                        "analyzer": "semi_colon_analyzer",
                        "search_analyzer": "semi_colon_analyzer"
                    }
                }
            }
        }
    }

我为这3个字段定义了自定义分析器

  • dkncourseintakes(索引像&#34;三个月;三个月;三个月;三个月和三十四岁)
  • dkncourselocations(索引类似&#34; Burwood; Cloud Campus&#34;)
  • dknpagetagia(索引像&#34; ia-Business,ia-English,ia-Humanities,ia-Law&#34;)

另外,有人可以建议如何确认使用自定义分析器进行搜索吗?即&#39; semi_colon_analyzer&#39;就我而言

感谢阅读到目前为止,如果有人能帮助我弄清楚如何使这项工作,我真的很感激。

1 个答案:

答案 0 :(得分:0)

别介意的人。看起来映射是不正确的......就像这样......

"semi_colon_analyzer": {
                        "type": "pattern",
                        "pattern": ";", <=== This was the problem
                        "lowercase": false
                    }

将其更改为"pattern": "; "(仅在分号后添加空格,现在全部都好)