当前位置: 首页 > 知识库问答 >
问题:

从elasticsearch响应中排除字段

邢新
2023-03-14

如果我们有如下查询

{
"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”中排除与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个答案

卓胜
2023-03-14

我要找的是https://stackoverflow.com/a/40780938/1331769-一种从嵌套类型字段中排除所有字段但只返回匹配字段的方法

 类似资料:
  • 嗨,我是ElasticSearch的新手,我正在努力从ElasticSearch的响应中删除所有额外字段。请参见以下回复: 上面你可以看到响应中有一些附加字段,如_index、_type、_score、max_score等。我按照ElasticSearchresponse_filtering指南删除了这些字段,但对我不起作用。我还将filter_path参数传递给请求0.0.0.0:5000/v4

  • 我正在开发运动衫2 RESTful服务 球衣2 v2。22.2 Spring3 v3。2.16 Jax rs v2。0.1 如果我收到@QueryParam template=“summary”,我需要排除一些GameDTO响应字段。 例如: 获取/Rest/v1/游戏?city=1必须返回: 然而 获取/Rest/v1/游戏?城市=1?模板=必须返回摘要: 如何排除字段?

  • ****TransactionHistoryBO POJO** ** 事务历史响应pojo

  • 问题内容: 我正在执行一个查询,返回的查询类似于70k文档(我需要所有文档,我目前正在使用扫描和滚动) 发生的结果是响应非常大(2 MB,我们已经将其从6 MB减少了)。我们筛选了所需的字段,由于仅从API调用查询,因此我们减少了属性的名称。 我可以看到的是,数组“命中”中的每个文档都具有以下我实际上不需要的默认字段: _index(我们仅要求一个索引) _type(我们只要求一种类型) _id(

  • 我想从ElasticSearch的结果文档中排除一个字段。我浏览了ElasticSearch.org的这个文档http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html但当我尝试时,这不起作用。我在SO上看到了同样的问题。有没有办法在Elasticsearch查询中

  • 问题内容: 具有以下映射: 我在上面放了两个项目: 现在,我想在每个字段上搜索,但是,所以我要做的是: 它应该只显示该项目,因为第二个项目带有单词,但是它在上,这就是我要忽略的内容。如何存档? PS:这只是一个简单的示例,我有一个包含很多属性的映射,我想在某些搜索中忽略其中的一些属性。 PS2:我正在使用ES 2.3.2 问题答案: 该设置仅在不返回响应中的字段时才有用,但这并不排除搜索该字段。