简体   繁体   English

Elasticsearch:如何仅检索所需的嵌套对象

[英]Elasticsearch : How to retrieve only the desired nested objects

I have the below mapping structure for my Elasticsearch index. 我的Elasticsearch索引具有以下映射结构。

{
  "users": {
    "mappings": {
      "user-type": {
        "properties": {
          "lastModifiedBy": {
            "type": "string"
          },
          "lastModifiedDate": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "details": {
            "type": "nested",
            "properties": {
              "lastModifiedBy": {
                "type": "string"
              },
              "lastModifiedDate": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "views": {
                "type": "nested",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "properties": {
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "type": {
                        "type": "string"
                      },
                      "value": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Basically I want to retrieve ONLY the view object inside details based on index id & view id(details.views.id). 基本上我只想基于索引ID和视图ID(details.views.id)检索详细信息内的视图对象。

I have tried with the below java code.But seems to be not working. 我已经尝试使用下面的Java代码。但似乎无法正常工作。

SearchRequestBuilder srq =  this.client.prepareSearch(this.indexName)
    .setTypes(this.type)
    .setQuery(QueryBuilders.termQuery("_id", sid))
    .setPostFilter(FilterBuilders.nestedFilter("details.views", 
         FilterBuilders.termFilter("details.views.id", id)));

Below is the query structure for this java code. 以下是此Java代码的查询结构。

{
  "query": {
    "term": {
      "_id": "123"
    }
  },
  "post_filter": {
    "nested": {
      "filter": {
        "term": {
          "details.views.id": "def"
        }
      },
      "path": "details.views"
    }
  }
}

Since details is nested and view is nested inside details , you basically need two nested filters as well (one for each level) + the constraint on the _id field is best done with the ids query . 由于detailsnestedview是嵌套在details ,因此,您基本上还需要两个nested过滤器(每个级别一个)+ _id字段的约束最好由ids查询完成 The query DSL would look like this: 查询DSL如下所示:

{
  "query": {
    "ids": {
      "values": [
        "123"
      ]
    }
  },
  "post_filter": {
    "nested": {
      "filter": {
        "nested": {
          "path": "details.view",
          "filter": {
            "term": {
              "details.views.id": "def"
            }
          }
        }
      },
      "path": "details"
    }
  }
}

Translating this into Java code yields: 将其转换为Java代码会产生:

// 2nd-level nested filter
FilterBuilder detailsView = FilterBuilders.nestedFilter("details.views", 
    FilterBuilders.termFilter("details.views.id", id));

// 1st-level nested filter
FilterBuilder details = FilterBuilders.nestedFilter("details", detailsView);

// ids constraint
IdsQueryBuilder ids = QueryBuilders.idsQuery(this.type).addIds("123");

SearchRequestBuilder srq =  this.client.prepareSearch(this.indexName)
   .setTypes(this.type)
   .setQuery(ids)
   .setPostFilter(details);

PS: I second what @Paul said, ie always play around with the query DSL first and when you know you have zeroed in on the exact query you need, then you can translate it to the Java form. PS:我@Paul说了第二句话,即始终先处理查询DSL,并且当您知道已对所需的确切查询进行了归零后,便可以将其转换为Java形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM