elasticsearch elasticsearch查找数组中给定数字项具有相同属性值的文档,elasticsearch,elasticsearch" /> elasticsearch elasticsearch查找数组中给定数字项具有相同属性值的文档,elasticsearch,elasticsearch" />

elasticsearch elasticsearch查找数组中给定数字项具有相同属性值的文档

elasticsearch elasticsearch查找数组中给定数字项具有相同属性值的文档,elasticsearch,elasticsearch,首先,我想展示一下文档的简化结构 { "_id": "413123123", "_source": { "description": { "firstLine": "this is my description", "secondLine": "some value" }, &quo

首先,我想展示一下文档的简化结构

{
  "_id": "413123123",
  "_source": {
    "description": {
      "firstLine": "this is my description",
      "secondLine": "some value"
    },
    "InsertDetails": {
      "Timestamp": "2020-06-12T11:14:36+0000"
    },
    "Links": [
      {
        "LinkDetails": {
          "linkId": 2342,
          "type": "Link",
          "dateCreation": "2012-09-21T08:42:09+0000",
          "typeId": 404019,
          "typeOfLink": "http"
        }
      },
      {
        "LinkDetails": {
          "linkId": 321313,
          "type": "Link",
          "dateCreation": "2012-08-21T08:42:09+0000",
          "typeId": 404019,
          "typeOfLink": "http"
        }
      },
      {
        "LinkDetails": {
          "linkId": 1231,
          "type": "Link",
          "dateCreation": "2012-09-21T08:42:09+0000",
          "typeId": 32323,
          "typeOfLink": "https"
        }
      },
      {
        "LinkDetails": {
          "linkId": 53434,
          "type": "Link",
          "dateCreation": "2012-11-21T08:42:09+0000",
          "typeId": 123231,
          "typeOfLink": "wss"
        }
      }
    ]
  }
}
我在形成查询时遇到问题,该查询将查找满足以下要求的文档:

  • 链接数组中的两个项的typeOfLink等于http
  • 描述字符串包含单词“this”
  • 找到的项目将按日期描述排序
elasticsearch的版本是2.3.2

我尝试过这样的查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "Links.LinkDetails.typeOfLink": "http"
                                }
                            }
                        ],
                        "minimum_should_match": 2
                    }
                },
                {
                    "match": {
                        "description.firstLine": "this"
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "InsertDetails.Timestamp": {
                "order": "desc"
            }
        }
    ]
}
问题是这个查询还返回文档,在数组中只有一个具有给定值的项。我试图用不同的方式修改这个查询,但没有任何运气

添加映射

{
    "my_index": {
        "mappings": {
            "en": {
                "properties": {
                    "InsertDetails": {
                        "properties": {
                            "Timestamp": {
                                "type": "date",
                                "format": "strict_date_optional_time||epoch_millis"
                            }
                        }
                    },
                    "description": {
                        "properties": {
                            "firstLine": {
                                "type": "string"
                            },
                            "secondLine": {
                                "type": "string"
                            }
                        }
                    },
                    "Links": {
                        "properties": {
                            "LinkDetails": {
                                "properties": {
                                    "linkId": {
                                        "type": "long"
                                    },
                                    "type": {
                                        "type": "string"
                                    },
                                    "dateCreation": {
                                        "type": "date",
                                        "format": "strict_date_optional_time||epoch_millis"
                                    },
                                    "typeOfLink": {
                                        "type": "string"
                                    },
                                    "typeId": {
                                        "type": "long"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

首先,您希望对嵌套字段进行筛选。(对象数组) 要获得一致的结果,必须将此字段映射为嵌套字段。

然后,您将不得不使用聚合。 您想要的是只聚合类型为\u的\u链接的“http”值,如果聚合返回的结果超过2个,则返回结果

您的查询将稍微复杂一些:

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "Links",
            "query": {
              "match": {
                "Links.LinkDetails.typeOfLink": "http"
              }
            }
          }
        },
        {
          "match": {
            "description.firstLine": "this"
          }
        }
      ]
    }
  },
  "aggs": {
    "links": {
      "nested": {
        "path": "Links"
      },
      "aggs": {
        "http_only": {
          "filter": {
            "term": {
              "Links.LinkDetails.typeOfLink.keyword": "http"
            }
          },
          "aggs": {
            "several_http": {
              "terms": {
                "field": "Links.LinkDetails.typeOfLink.keyword",
                "min_doc_count": 2
              }
            ,
              "aggs": {
                "complete_match": {
                  "top_hits": {
                    "size": 100
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "sort": [
    {
      "InsertDetails.Timestamp": {
        "order": "desc"
      }
    }
  ]
}
您的回答如下所示:

"aggregations" : {
    "links" : {
      "doc_count" : 4,
      "http_only" : {
        "doc_count" : 2,
        "several_http" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "http",
              "doc_count" : 2,
              "complete_match" : {
                "hits" : {
                  "total" : {
                    "value" : 2,
                    "relation" : "eq"
                  },
                  "max_score" : 0.98082924,
                  "hits" : [
                    {
                      "_index" : "test3",
                      "_type" : "_doc",
                      "_id" : "ed1AkXQBD_dLYq-V78bD",
                      "_nested" : {
                        "field" : "Links",
                        "offset" : 0
                      },
                      "_score" : 0.98082924,
                      "_source" : {
                        "LinkDetails" : {
                          "linkId" : 2342,
                          "type" : "Link",
                          "dateCreation" : "2012-09-21T08:42:09+0000",
                          "typeId" : 404019,
                          "typeOfLink" : "http"
                        }
                      }
                    },
                    {
                      "_index" : "test3",
                      "_type" : "_doc",
                      "_id" : "ed1AkXQBD_dLYq-V78bD",
                      "_nested" : {
                        "field" : "Links",
                        "offset" : 1
                      },
                      "_score" : 0.98082924,
                      "_source" : {
                        "LinkDetails" : {
                          "linkId" : 321313,
                          "type" : "Link",
                          "dateCreation" : "2012-08-21T08:42:09+0000",
                          "typeId" : 404019,
                          "typeOfLink" : "http"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }

通过使用给定的聚合,您应该能够执行您想要的操作。

您可以共享您的ES索引映射吗?添加了索引映射