从elasticsearch响应中排除字段

时间:2018-04-11 02:14:30

标签: elasticsearch

如果我们有如下查询

{
"from" : 0,
"size" : 1000,
"_source": {
                  "includes": [],
                  "excludes": ["tenantId", "instance.grantedPEIds", "instance.entitlements",
                    "instance.taskAssignees", 
                    "instance.relatedInstances"

                  ]
                },
"query" : {
"bool" : {
  "filter" : {
    "bool" : {
      "must" : [ {
        "bool" : {
          "must" : [ {
            "bool" : {
              "must" : [ {
                "term" : {
                  "instance.dataSourceId" : 560
                }
              }, {
                "term" : {
                  "instance.dsTypeId" : 1789
                }
              }, {
                "bool" : {
                  "must_not" : {
                    "term" : {
                      "instance.status" : 6
                    }
                  }
                }
              } ]
            }
          }, {
            "bool" : {
              "must" : {
                "nested" : {
                  "query" : {
                    "filtered" : {
                      "query" : {
                        "match_all" : { }
                      },
                      "filter" : {
                        "bool" : {
                          "must" : [ {
                            "term" : {
                              "instance.instFields.sourceFieldId" : "9925"
                            }
                          }, {
                            "bool" : {
                              "should" : {
                                "match" : {
                                  "instance.instFields.fieldValue" : {
                                    "query" : "2b2f94b5-9231-44f9-b371-ffd7fd56b38e",
                                    "type" : "boolean"
                                  }
                                }
                              }
                            }
                          } ]
                        }
                      }
                    }
                  },
                  "path" : "instance.instFields"
                }
              }
            }
          }, {
            "bool" : {
              "must" : {
                "nested" : {
                  "query" : {
                    "filtered" : {
                      "query" : {
                        "match_all" : { }
                      },
                      "filter" : {
                        "bool" : {
                          "must" : [ {
                            "term" : {
                              "instance.instFields.sourceFieldId" : "9954"
                            }
                          }, {
                            "bool" : {
                              "should" : [ {
                                "match" : {
                                  "instance.instFields.fieldValue" : {
                                    "query" : "2caef68d-f581-4376-a624-08ee912a52fa",
                                    "type" : "boolean"
                                  }
                                }
                              }, {
                                "match" : {
                                  "instance.instFields.fieldValue" : {
                                    "query" : "33ce8f72-762a-4891-99aa-f053b5a08b05",
                                    "type" : "boolean"
                                  }
                                }
                              } ]
                            }
                          } ]
                        }
                      }
                    }
                    },
                    "path" : "instance.instFields"
                  }
                }
              }
            } ]
          }
        } ]
      }
    }
  }
}

}

按预期返回结果(如下所示)

{
  "took": 822,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 270,
    "max_score": null,
    "hits": [
      {
        "_index": "dsi2",
        "_type": "dsi2",
        "_id": "110199",
        "_score": null,
        "_source": {
          "instance": {

            "dataSourceId": 560,
            "dsTypeId": 1789,
            "modifiedTs": 1521733917638,
            "instanceId": 7654,
            "instFields": [
              {
                "fieldDouble": 18,
                "sourceFieldId": 9914,
                "sortOrder": 0,
                "isKey": false,
                "ddTypeId": 13,
                "fieldValue": "18",
                "fieldString": "18"
              },
              {
                "optionLabel": "In Progress",
                "sourceFieldId": 9954,
                "sortOrder": 1,
                "isKey": true,
                "ddTypeId": 34,
                "fieldValue": "2caef68d-f581-4376-a624-08ee912a52fa",
                "fieldString": "2caef68d-f581-4376-a624-08ee912a52fa"
              },
              {
                "sourceFieldId": 9537,
                "sortOrder": 0,
                "isKey": false,
                "ddTypeId": 21,
                "fieldValue": "none",
                "fieldString": "NONE"
              },
              {
                "sourceFieldId": 9930,
                "sortOrder": 0,
                "isKey": false,
                "ddTypeId": 1,
                "fieldValue": "test",
                "fieldString": "test"
              },
              {
              "sourceFieldId": 9945,
              "sortOrder": 0,
            "isKey": false,
            "fieldBoolean": false,
            "ddTypeId": 6,
            "fieldValue": "false",
            "fieldString": "false"
          },
          {
            "sourceFieldId": 10045,
            "sortOrder": 0,
            "isKey": false,
            "fieldBoolean": false,
            "ddTypeId": 54,
            "fieldValue": "false",
            "fieldString": "false"
          },
          {
            "sourceFieldId": 9982,
            "sortOrder": 0,
            "isKey": false,
            "fieldBoolean": false,
            "ddTypeId": 6,
            "fieldValue": "false",
            "fieldString": "false"
          },
          {
            "optionLabel": "Yard",
            "sourceFieldId": 9925,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 1,
            "fieldValue": "2b2f94b5-9231-44f9-b371-ffd7fd56b38e",
            "fieldString": "2b2f94b5-9231-44f9-b371-ffd7fd56b38e"
          },
          {
            "sourceFieldId": 9922,
            "sortOrder": 0,
            "isKey": true,
            "ddTypeId": 35,
            "fieldValue": "testvin4mike",
            "fieldString": "TestVIN4Mike"
          },
          {
            "fieldDouble": 9,
            "sourceFieldId": 9932,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 13,
            "fieldValue": "9.0",
            "fieldString": "9.0"
          },
          {
            "sourceFieldId": 9915,
            "sortOrder": 0,
            "isKey": false,
            "fieldDate": 1508443519489,
            "ddTypeId": 45,
            "fieldValue": "1508443519489",
            "fieldString": "1508443519489"
          },
          {
            "fieldDouble": 7654,
            "sourceFieldId": 9923,
            "sortOrder": 1,
            "isKey": false,
            "ddTypeId": 22,
            "fieldValue": "7654",
            "fieldString": "7654"
          },
          {
            "optionLabel": "p",
            "sourceFieldId": 9942,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 41,
            "fieldValue": "p",
            "fieldString": "p"
          }
        ],
        "processId": "680508",
        "tenantId": 132,
        "formDefinitionId": 2389,
        "id": 110199,
        "status": 4
      },
      "formTypeId": 3,
      "status": 4
    },
    "sort": [
      "1508443519489"
    ]
  }
]
}
}

正如您可以看到查询适用于"instance.instFields.sourceFieldId = 9925"instance.instFields.sourceFieldId" : "9954"我们要从结果中排除其他字段,但只包含sourceFieldId=9954sourceFieldId=9925

的字段
{
            "optionLabel": "In Progress",
            "sourceFieldId": 9954,
            "sortOrder": 1,
            "isKey": true,
            "ddTypeId": 34,
            "fieldValue": "2caef68d-f581-4376-a624-08ee912a52fa",
            "fieldString": "2caef68d-f581-4376-a624-08ee912a52fa"
          }

{
            "optionLabel": "Yard",
            "sourceFieldId": 9925,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 1,
            "fieldValue": "2b2f94b5-9231-44f9-b371-ffd7fd56b38e",
            "fieldString": "2b2f94b5-9231-44f9-b371-ffd7fd56b38e"
          },

并排除其他字段,例如以下是其中一些不是sourceFieldId = 9925sourceFieldId= 9954(传递给查询的内容) - 从"instFields": []排除字段的最佳方式是什么? t匹配查询中的sourceFieldId

{
            "sourceFieldId": 9537,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 21,
            "fieldValue": "none",
            "fieldString": "NONE"
          },
          {
            "sourceFieldId": 9930,
            "sortOrder": 0,
            "isKey": false,
            "ddTypeId": 1,
            "fieldValue": "test",
            "fieldString": "test"
          } 

1 个答案:

答案 0 :(得分:0)

what I was looking for is https://stackoverflow.com/a/40780938/1331769 - a way to exclude all of the fields from a nested type field but return only the matching one