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



这里是弹性新手。我在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 ..."
}

在日期范围[2020-01-012020-01-30]中搜索cups时,应返回blog_id1,但不应返回blog_id2。

这是对问题建模的正确方法吗?还是应该使用nested对象来进行更简单的查询?

这能在基巴纳制成一份报告吗?

这可以通过使用下面的一组查询来完成,这些查询被整理成一个ES请求。

实现这一点的方法是,首先根据timestamp(即通过范围查询(过滤文档,并发布您可以应用Term Queries的消息,这就像选择不同的等价物一样,然后您可以在Terms Query中添加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.

以下是示例响应:

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

最新更新