ElasticSearch布尔查询结果不匹配

时间:2014-08-22 10:15:32

标签: java elasticsearch lucene boolean-logic nosql

这是我的索引映射

   "index":{
      "mappings":{
         "patient":{
            "properties":{
               "LastName":{
                  "type":"string"
               },
               "accountType":{
                  "type":"string"
               },
               "civilStatus":{
                  "type":"string"
               },
               "consultations":{
                  "type":"nested",
                  "properties":{
                     "deleted":{
                        "type":"boolean"
                     },
                     "diagnosis":{
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "documentDate":{
                        "type":"date",
                        "format":"dateOptionalTime"
                     },
                     "firstName":{
                        "type":"string"
                     },
                     "lastName":{
                        "type":"string"
                     },
                     "middleName":{
                        "type":"string"
                     },
                     "prescriptions":{
                        "type":"string"
                     }
                  }
               },
               "firstName":{
                  "type":"string"
               },
               "gender":{
                  "type":"string"
               },
               "id":{
                  "type":"string",
                  "index":"not_analyzed"
               },
               "lastName":{
                  "type":"string"
               },
               "middleName":{
                  "type":"string"
               },
               "occupation":{
                  "type":"string"
               },
               "owner":{
                  "type":"string",
                  "index":"not_analyzed"
               },
               "patientPin":{
                  "type":"string"
               }
            }
         }
      }
   }
}

这是ElasticSearch唯一保存的数据

{
   "_index":"index",
   "_type":"patient",
   "_id":"TENANT1100066",
   "_score":1.0,
   "_source":{
      "id":"100066",
      "firstName":"Johnny",
      "patientPin":"201408000001",
      "middleName":"John ",
      "consultations":[
         {
            "id":null,
            "prescriptions":[

            ],
            "diagnosis":[
               "headache of unknown origin"
            ],
            "documentDate":"2014-08-05T10:10:00.000+08:00",
            "deleted":false,
            "lastName":"David",
            "firstName":"Johnny ",
            "middleName":"John "
         }
      ],
      "owner":"TENANT1",
      "gender":"MALE",
      "occupation":"Unspecified",
      "accountType":"INDIVIDUAL",
      "civilStatus":"SINGLE",
      "lastName":"David"
   }
}

这是我为检查布尔查询的工作原理而构建的示例查询。

{
  "nested" : {
    "query" : {
      "bool" : {
        "must" : [ {
          "match" : {
            "consultations.diagnosis" : {
              "query" : "Kawasaki's Disease",
              "type" : "phrase"
            }
          }
        }, {
          "match" : {
            "consultations.diagnosis" : {
              "query" : "Alcohol Intoxication",
              "type" : "phrase"
            }
          }
        } ],
        "must_not" : {
          "match" : {
            "consultations.deleted" : {
              "query" : "true",
              "type" : "boolean"
            }
          }
        },
        "should" : {
          "match" : {
            "consultations.diagnosis" : {
              "query" : "headache of unknown origin",
              "type" : "phrase"
            }
          }
        }
      }
    },
    "path" : "consultations"
  }

显然,川崎病和纤维变性不存在,但存在不明原因的头痛,但没有 返回结果(Johnny John David)我在这里错过了什么?我心中的操作是 (川崎病和纤维化)或者来源不明的头痛。

我的想法是,如果没有患有川崎病和纤维变性的患者寻找患有未知来源的头痛的患者"。我们显然有,但我的查询返回0结果。我在这里失踪了什么

1 个答案:

答案 0 :(得分:1)

在您的查询中,当您在必须子句中添加这两个条件时,您需要匹配的文档同时具有(川崎病和Fibriasis)。

您的文档仅与子句匹配,因此不会出现在搜索结果中。

实现您的目标:

(Kawasaki's Disease AND Fibriasis) OR headache of unknown origin

您可以将这两种疾病嵌入到另一个bool查询中,并在根查询的should部分添加此查询,如下所示:

{
  "query": {
    "nested": {
      "path": "consultations",
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [{
                  "match_phrase": {
                    "consultations.diagnosis": "Kawasaki's Disease"
                  }
                },
                {
                  "match_phrase": {
                    "consultations.diagnosis": "Alcohol Intoxication"
                  }
                }
                ]
              }
            },
            {
              "match_phrase": {
                "consultations.diagnosis": "headache of unknown origin"
              }
            }
          ],
          "minimum_number_should_match": 1
        }
      }
    }
  }
}

输出先前索引的患者:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.3007646,
      "hits": [
         {
            "_index": "test",
            "_type": "patient",
            "_id": "TENANT1100066",
            "_score": 0.3007646,
            "_source": {
               "id": "100066",
               "firstName": "Johnny",
               "patientPin": "201408000001",
               "middleName": "John ",
               "consultations": [
                  {
                     "id": null,
                     "prescriptions": [],
                     "diagnosis": [
                        "headache of unknown origin"
                     ],
                     "documentDate": "2014-08-05T10:10:00.000+08:00",
                     "deleted": false,
                     "lastName": "David",
                     "firstName": "Johnny ",
                     "middleName": "John "
                  }
               ],
               "owner": "TENANT1",
               "gender": "MALE",
               "occupation": "Unspecified",
               "accountType": "INDIVIDUAL",
               "civilStatus": "SINGLE",
               "lastName": "David"
            }
         }
      ]
   }
}
相关问题