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

disbfnqx  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(294)

首先我想展示一下文件的简化结构。

{
  "_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"
            }
        }
    ]
}

问题是这个查询还返回文档,在数组中只有一项具有给定值。我尝试过用不同的方式修改这个查询,但是没有任何运气。
添加的Map

{
    "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"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
wbgh16ku

wbgh16ku1#

首先,要对嵌套字段进行筛选(对象数组)要获得一致的结果,必须将此字段Map为嵌套字段。https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
然后,您将不得不使用聚合。您想要的是只聚合类型为\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"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }

通过使用给定的聚合,您应该能够做您想做的事情。

相关问题