繁体   English   中英

Elasticsearch -- 计算文档中关键字出现的次数

[英]Elasticsearch -- count number of keyword occurences in a document

数据库:Elasticsearch v7.2
应用:Laravel v5.7
使用 Elasticsearch/Elasticsearch ( https://github.com/elastic/elasticsearch-php ) 官方 PHP 库

我有一个用于 Elasticsearch 的 query_string 查询,使用此代码检索具有特定短语的文档,因为我在整个索引中搜索

[
    "query_string" => [
        "default_field" => $content,
        "query" => $keywords
    ]
],

$keywords变量包含:

("MCU" OR "Marvel" OR "Spiderman")

现在,我要算这些话OCCURENCES数量的文档,我要找回

我使用了 aggs 查询:

'aggs' => [
    'count' => [
        'terms' => [
            'field' => 'content.keyword'
        ]
    ]
]

但是,我不知道如何将这些doc_count关联doc_count并以匹配的方式显示它与点击量 - 因为键本身是内容,而不是 ID

在此处输入图片说明

我打算显示整个文档,并将上面的$keywords在每个文档中出现的次数作为Mentions

在此处输入图片说明 是否有其他方法可以在不使用 Elasticsearch 中的aggs的情况下计算出现次数?

如果您只想计算关键字的出现次数,则不必启用 fielddata,请尝试使用过滤器 aggs 以及您的查询

GET my_index/_search
{
  "query": {
    "query_string": {
      "default_field": "content", 
      "query": "MCU OR Marvel OR Spiderman"
    }
  },
  "aggs": {
    "count": {
      "filters": {
        "filters": {
          "mcu": {
            "match": {
              "content": "MCU"
            }
          },
          "marvel": {
            "match": {
              "content": "Marvel"
            }
          },
          "spiderman": {
            "match": {
              "content": "Spiderman"
            }
          }
        }
      }
    }
  }
}

结果如下:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1.219939,
    "hits": [
      ....
      ....
    ]
  },
  "aggregations": {
    "count": {
      "buckets": {
        "marvel": {
          "doc_count": 2
        },
        "mcu": {
          "doc_count": 2
        },
        "spiderman": {
          "doc_count": 1
        }
      }
    }
  }
}

来源: https : //www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html

感谢@AshrafulIslam 先生,我能够想出 Elasticsearch 的名为highlights的功能。 尽管highlights字面上强调出现的关键字,但我使用了 PHP 的substr_count()函数来计算<em>标签

我将此代码添加为['body']['query']元素的同级:

"highlight" => [
    "fields" => [
        "content" => ["number_of_fragments" => 0]
    ],
    'require_field_match' => false
]

然后,当我遍历['hits']['hits']数组元素时,我执行了如下操作:

$articles = $client->search($params);
$hits = $articles['hits']['hits'];

for($i=0; $i<count($hits); $i++){
    $hits[$i]['_source']['count_mentions'] = substr_count($hits[$i]['highlight']['content'][0],"<em>");
}

启用 Fielddata 可能不是启用文本搜索的最佳方式。

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html#before-enabling-fielddata

在启用 fielddata 之前,请考虑为什么要使用文本字段进行聚合、排序或在脚本中。 这样做通常没有意义。

在索引之前分析文本字段,以便可以通过搜索 new 或 york 找到像 New York 这样的值。 当您可能需要一个名为 New York 的存储桶时,此字段上的术语聚合将返回一个新存储桶和一个 york 存储桶。

相反,您应该有一个用于全文搜索的文本字段,以及一个为聚合启用 doc_values 的未分析关键字字段,如下所示:

PUT my_index
{
  "mappings": {
    "properties": {
      "my_field": { 
        "type": "text",
        "fields": {
          "keyword": { 
            "type": "keyword"
          }
        }
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM