1. 程式人生 > >es dsl 空值和有值的匹配

es dsl 空值和有值的匹配

  • 非空匹配

返回在原始欄位中至少有一個非空值的文件:

比如下面的欄位都因為有值而被匹配

{ "user": "jane" }

{ "user": "" } ①

{ "user": "-" } ②

{ "user": ["jane"] }

{ "user": ["jane", null ] } ③

demo比如:匹配所有ownerId 的值

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "businessId": "10"
          }
        },
        {
          "exists" : {
            "field" : "ownerId"
          }
        }
      ],
      "should": [],
      "filter": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": []
}

 

返回值:看ownerId 

{
  "took": 79,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2977535,
    "max_score": 2,
    "hits": [
      {
        "_index": "csc-pacific-case-monitor",
        "_type": "case-monitor",
        "_id": "36501315",
        "_score": 2,
        "_source": {
          "caseId": "36501315",
          "addTime": 1537100564000,
          "caseTypeId": "776",
          "closerId": "17821",
          "closeTime": 1537100564000,
          "contactNumber": "[\"\",\"17764105080\"]",
          "content": "無人接單",
          "creatorId": "17821",
          "customerId": "115962670",
          "customerType": "1001",
          "deadline": 1537102364000,
          "feedbackTime": null,
          "businessId": "10",
          "businessName": null,
          "fourthCategoryId": null,
          "fifthCategoryId": null,
          "ownerId": "17821",
          "managerId": "17821",
          "questionId": "9984",
          "questionReasonId": null,
          "orderTypeId": "8",
          "source": "2",
          "statusId": "2",
          "subSource": "-1",
          "questionTypeId": "2767",
          "updateTime": 1537100564000,
          "startTime": 1537100564000,
          "priorityId": "7",
          "updaterId": "17821",
          "memo": null,
          "flowId": "533",
          "nodeId": "67266",
          "lifeCycle": "1",
          "latest_main_scn": 4900984951192,
          "latest_main_es_update_time": 1537100564740,
          "level_1": "1",
          "level_2": "3",
          "level_3": "6",
          "level_4": "11",
          "level_5": "621",
          "level_6": "622",
          "departmentId": 622
        }
      
      }
    ]
  }
}

 

  • 空值匹配

這些文件將不會被上面的查詢匹配到:

"user"null }

"user": [] } ①

"user": [null] } ②

"foo":  "bar" } ③

①這個欄位沒有任何值。

②至少需要一個 non-null 非空值。

③ user 欄位完全丟失。

下面的demo,是匹配了上面的情況

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "businessId": "10"
          }
        }
      ],
      "must_not": [ 
        {
           "exists" : {
              "field" : "ownerId"
           }
        }
      ],
      "should": [],
      "filter": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": []
}

返回結果如下

{
  "took": 52,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 88888,
    "max_score": 1,
    "hits": [
      {
        "_index": "csc-pacific-case-monitor",
        "_type": "case-monitor",
        "_id": "36499324",
        "_score": 1,
        "_source": {
          "caseId": "36499324",
          "addTime": 1537099751000,
          "caseTypeId": "100",
          "closerId": "0",
          "closeTime": 1537100911000,
          "contactNumber": null,
          "content": null,
          "creatorId": "0",
          "customerId": "340327304",
          "customerType": "1001",
          "deadline": 1537186151000,
          "feedbackTime": null,
          "businessId": "10",
          "businessName": null,
          "fourthCategoryId": null,
          "fifthCategoryId": null,
          "ownerId": null,
          "managerId": "0",
          "questionId": "3962",
          "questionReasonId": null,
          "orderTypeId": "8",
          "source": "20",
          "statusId": "2",
          "subSource": "301",
          "questionTypeId": "2770",
          "updateTime": 1537100911000,
          "startTime": 1537099751000,
          "priorityId": "5",
          "updaterId": "0",
          "memo": null,
          "flowId": "118",
          "nodeId": "67975",
          "lifeCycle": "1",
          "latest_main_scn": 4901075798318,
          "latest_main_es_update_time": 1537100911065
        }
      },
      {
        "_index": "csc-pacific-case-monitor",
        "_type": "case-monitor",
        "_id": "36509054",
        "_score": 1,
        "_source": {
          "caseId": "36509054",
          "addTime": 1537104132000,
          "caseTypeId": "100",
          "closerId": "0",
          "closeTime": 1537105346000,
          "contactNumber": null,
          "content": null,
          "creatorId": "0",
          "customerId": "218704968",
          "customerType": "1001",
          "deadline": 1537190532000,
          "feedbackTime": null,
          "businessId": "10",
          "businessName": null,
          "fourthCategoryId": null,
          "fifthCategoryId": null,
          "ownerId": null,
          "managerId": "0",
          "questionId": "3962",
          "questionReasonId": null,
          "orderTypeId": "8",
          "source": "20",
          "statusId": "2",
          "subSource": "301",
          "questionTypeId": "2770",
          "updateTime": 1537105346000,
          "startTime": 1537104132000,
          "priorityId": "5",
          "updaterId": "0",
          "memo": null,
          "flowId": "118",
          "nodeId": "67975",
          "lifeCycle": "1",
          "latest_main_scn": 4905281330698,
          "latest_main_es_update_time": 1537105346278
        }
      }
    ]
  }
}