elasticsearch - 在时间范围内弹性查找记录组中的文本

标签 elasticsearch

flex 新手。我以这种格式在ElasticSearch中存储博客文章:

{
  blog_id: keyword,
  blog_article_id: keyword,
  timestamp: date,
  article_text: text
}
假设我想在过去30天内找到所有2篇或以上有关X的不同博客。是否存在查询以查找在日期范围内具有相同词的不同文章的所有blog_id
例如:
{
  "blog_id": "1"
  "blog_article_id": 10,
  "timestamp": 2020-01-02T00:00:00,
  "article_text": "... cups ..."
},
{
  "blog_id": "1"
  "blog_article_id": 11,
  "timestamp": 2020-01-20T00:00:00,
  "article_text": "... cups ..."
},
{
  "blog_id": "2"
  "blog_article_id": 10,
  "timestamp": 2020-01-20T00:00:00,
  "article_text": "... cups ..."
}
在日期范围[cups2020-01-01]中搜索2020-01-30,应返回blog_id 1,但不返回blog_id 2。
这是对问题建模的正确方法,还是应该使用nested对象进行更轻松的查询?
可以在Kibana中将其记录为报告吗?

最佳答案

这可以通过使用以下查询集(整理到单个ES请求中)来完成。
要做到这一点,首先要基于timestamp过滤文档,即通过Range Query进行过滤,然后发布可以应用 Term Queries 的信息,就像选择不同种类的等价物一样,然后可以添加通过管道传输到术语查询的 Top Hits Aggregation 查询。

POST <your_index_name>/_size
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2020-01-01",
              "lte": "2020-01-30"
            }
          }
        },
        {
          "match": {
            "article_text": "cups"
          }
        }
      ]
    }
  },
  "aggs": {
    "my_blog_ids": {
      "terms": {
        "field": "blog_id",
        "size": 100,                          <---- Term Size
        "min_doc_count": 2
      },
      "aggs": {
        "my_document_hits": {
          "top_hits": {
            "size": 10
          }
        },
        "bucket_count": {                     <---- bucket count
          "value_count": {
            "field": "_id"
          }
        }
      }
    }
  }
}
在上面有两个提及。
第一个是仅获取聚合查询的结果,第二个是仅返回计数大于1的blog_id。
以下是示例响应:
{
  "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" : {
    "my_blog_ids" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1",                        <---- blog_id 1
          "doc_count" : 2,
          "my_document_hits" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.0,
              "hits" : [
                {
                  "_index" : "my_blog_index",
                  "_type" : "_doc",
                  "_id" : "1",                       <---- doc 1
                  "_score" : 1.0,
                  "_source" : {
                    "blog_id" : "1",
                    "blog_article_id" : 10,
                    "timestamp" : "2020-01-02T00:00:00",
                    "article_text" : "... cups ..."
                  }
                },
                {
                  "_index" : "my_blog_index",
                  "_type" : "_doc",
                  "_id" : "2",                       <---- doc 2
                  "_score" : 1.0,
                  "_source" : {
                    "blog_id" : "1",
                    "blog_article_id" : 11,
                    "timestamp" : "2020-01-20T00:00:00",
                    "article_text" : "... cups ..."
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

关于elasticsearch - 在时间范围内弹性查找记录组中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62864603/

相关文章:

c# - ElasticSearch & Nest - 错误的转换

symfony - Silex + FOQElasticaBundle

elasticsearch - Nest NamedFilters 需要 promise

elasticsearch - 对Cassandra数据进行ElasticSearch与将Cassandra数据移至ElasticSearch进行索引

real-time - 刷新Elastic Search索引/实时搜索

ElasticSearch - 按特定字段删除文档

mysql - 全文检索数据库和超过 2 亿条记录

java - Elasticsearch:如何根据匹配条件删除条目

elasticsearch - 是否可以在 ElasticSearch 的 '_id' 中包含 '_source'

ruby-on-rails - 获得Tire::Results::Collection的随机结果