elasticsearch 循环访问doc以根据匹配返回数组中特定键的值

pzfprimi  于 2022-11-28  发布在  ElasticSearch
关注(0)|答案(1)|浏览(201)

数据库

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1000,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "learn",
        "_id": "OeCLr4QBPMAw7FiXknKz",
        "_score": 1,
        "_source": {
          "user_rating_size": 80,
          "ratingdescription": 80,
          "rating": "PG-13",
          "release_year": 2004,
          "user_rating_score": 82,
          "title": "White Chicks",
          "ratinglevel": "crude and sexual humor, language and some drug content"
        }
      },
      {
        "_index": "learn",
        "_id": "QuCLr4QBPMAw7FiXknKz",
        "_score": 1,
        "_source": {
          "user_rating_size": 80,
          "ratingdescription": 90,
          "rating": "TV-14",
          "release_year": 2016,
          "user_rating_score": 96,
          "title": "Pretty Little Liars",
          "ratinglevel": "Parents strongly cautioned. May be unsuitable for children ages 14 and under."
        }
      }
    ]
  }
}

Map

{
  "learn": {
    "mappings": {
      "_meta": {
        "created_by": "file-data-visualizer"
      },
      "properties": {
        "rating": {
          "type": "keyword"
        },
        "ratingdescription": {
          "type": "long"
        },
        "ratinglevel": {
          "type": "text"
        },
        "release_year": {
          "type": "long"
        },
        "title": {
          "type": "text"
        },
        "user_rating_score": {
          "type": "long"
        },
        "user_rating_size": {
          "type": "long"
        }
      }
    }
  }
}

我所要的只是基于评级匹配(分组)将title的所有值作为数组返回。
我试图根据评级对它进行分组,但它返回了匹配的文档。在这种情况下,我必须再次循环通过以只获得值。
总的来说,我从文档中看到的都是基于总和和其他统计数据的。
我也试着通过无痛脚本来做,但似乎找不出办法。

vql8enpb

vql8enpb1#

我必须在title中添加一个关键字字段类型,以便能够对其进行聚合:

PUT learn
{
  "mappings": {
    "_meta": {
      "created_by": "file-data-visualizer"
    },
    "properties": {
      "rating": {
        "type": "keyword"
      },
      "ratingdescription": {
        "type": "long"
      },
      "ratinglevel": {
        "type": "text"
      },
      "release_year": {
        "type": "long"
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "user_rating_score": {
        "type": "long"
      },
      "user_rating_size": {
        "type": "long"
      }
    }
  }
}

通过聚合

GET learn/_search
{
  "size": 0, 
  "query": {
    "match": {
      "title": "pretty"
    }
  },
  "aggs": {
    "ratings": {
      "terms": {
        "field": "rating",
        "size": 10
      },
      "aggs": {
        "titles": {
          "terms": {
            "field": "title.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "ratings": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "TV-14",
          "doc_count": 2,
          "titles": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Another Pretty TV-14 movie",
                "doc_count": 1
              },
              {
                "key": "Pretty Little Liars",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "PG-13",
          "doc_count": 1,
          "titles": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "Pretty White Chicks",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

通过折叠查询

GET learn/_search 
{
  "_source": false, 
  "query": {
    "match": {
      "title": "pretty"
    }
  },
  "collapse": {
    "field": "rating",
    "inner_hits": {
      "name": "titles",                  
      "size": 5,
      "_source": ["title"]
    }
  }
}

结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "learn",
        "_id": "JVV4vIQBtNG1OrZoVQ2v",
        "_score": 0.7361701,
        "fields": {
          "rating": [
            "TV-14"
          ]
        },
        "inner_hits": {
          "titles": {
            "hits": {
              "total": {
                "value": 2,
                "relation": "eq"
              },
              "max_score": 0.7361701,
              "hits": [
                {
                  "_index": "learn",
                  "_id": "JVV4vIQBtNG1OrZoVQ2v",
                  "_score": 0.7361701,
                  "_source": {
                    "title": "Pretty Little Liars"
                  }
                },
                {
                  "_index": "learn",
                  "_id": "_FV4vIQBtNG1OrZo-Q95",
                  "_score": 0.5897495,
                  "_source": {
                    "title": "Another Pretty TV-14 movie"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "learn",
        "_id": "wcV5vIQB5Gw0WET8ve-k",
        "_score": 0.7361701,
        "fields": {
          "rating": [
            "PG-13"
          ]
        },
        "inner_hits": {
          "titles": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.7361701,
              "hits": [
                {
                  "_index": "learn",
                  "_id": "wcV5vIQB5Gw0WET8ve-k",
                  "_score": 0.7361701,
                  "_source": {
                    "title": "Pretty White Chicks"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

相关问题