2

想知道是否有人可以提供帮助。

我有一个大致定义如下的 ElasticSearch 索引:

{
  "properties": {
    "content": {
      "type": "string"
    },
    "topics": {
      "properties": {
        "topic_type": {
          "type": "string"
        },
        "topic": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

因此,您最终会在索引中大致按照以下方式输入一个条目:

{
  "content": "some load of content",
  "timestamp": "some time stamp",
  "id": "some id",
  "topics": [
    {
      "topic": "safety",
      "topic_type": "Flight"
    },
    {
      "topic": "rockets",
      "topic_type": "Space"
    }
  ]
}

每个内容块可以有多个与之关联的主题。

我想做的是:按天汇总所有不同“空间”主题的计数,例如:

4月1日:

  • “火箭”:20
  • “宇航员”:2
  • “外星人”:5

4月2日:

  • “火箭”:10
  • “宇航员”:12
  • “外星人”:51

等等。

我试图做的是这样的:

curl -X POST 'http://localhost:9200/myindex/_search?search_type=count&pretty=true' -d '{
  "size": "100000",
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "myindex.topics.topic_type": "space"
          }
        }
      ]
    }
  },
  "aggs": {
    "articles_over_time": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "day"
      },
      "aggs": {
        "topics_over_time": {
          "terms": {
            "field": "topics.topic"
          }
        }
      }
    }
  }
}'

这样做的问题是,虽然它只选择了 topic_type 为“space”的那些文章,但其中一些文章将有其他“topics.topic”,这些“topics.topic”会在“aggs”位中被选中,即没有“空间”的主题类型。

我想要做的是说“计数和聚合[基本上]那些主题类型为'空间'的主题”。

所以在索引中只有这个:

{
  "content": "some load of content",
  "timestamp": "some time stamp",
  "id": "some id",
  "topics": [
    {
      "topic": "safety",
      "topic_type": "Flight"
    },
    {
      "topic": "rockets",
      "topic_type": "Space"
    }
  ]
}

这将是:火箭:1

在索引中有这两个:

{
  "content": "some load of content",
  "timestamp": "some time stamp",
  "id": "some id",
  "topics": [
    {
      "topic": "safety",
      "topic_type": "Flight"
    },
    {
      "topic": "rockets",
      "topic_type": "Space"
    }
  ]
}

{
  "content": "some load of content2",
  "timestamp": "some time stamp",
  "id": "some id",
  "topics": [
    {
      "topic": "safety",
      "topic_type": "Flight"
    },
    {
      "topic": "rockets",
      "topic_type": "Space"
    },
    {
      "topic": "aliens",
      "topic_type": "Space"
    }
  ]
}

这将是:rockets: 2, aliens: 1- 但都按天分组。

不知道如何用 ES 做到这一点。

如果索引模式在这里不适合,请让我知道(在您看来)是什么。

4

0 回答 0