【问题标题】:How to perform term aggregation based on Url Domain name using Nest ElasticClient如何使用 Nest ElasticClient 基于 Url 域名执行术语聚合
【发布时间】:2019-05-16 23:46:38
【问题描述】:

我想对 uri 字段执行聚合,但只返回 url 的域部分而不是完整的 url。例如,使用该字段,https://stackoverflow.com/questions/ask?guided=true 我会得到stackoverflow.com 给定一个现有的数据集如下:

"hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "L9WewGoBZqCeOmbRIMlV",
        "_score" : 1.0,
        "_source" : {
          "firstName" : "George",
          "lastName" : "Ouma",
          "pageUri" : "http://www.espnfc.com/story/683732/england-football-team-escaped-terrorist-attack-at-1998-world-cup",
          "date" : "2019-05-16T12:29:08.1308177Z"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "MNWewGoBZqCeOmbRIsma",
        "_score" : 1.0,
        "_source" : {
          "firstName" : "George",
          "lastName" : "Ouma",
          "pageUri" : "http://www.wikipedia.org/wiki/Category:Terrorism_in_Mexico",
          "date" : "2019-05-16T12:29:08.1308803Z"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "2V-ewGoBiHg_1GebJKIr",
        "_score" : 1.0,
        "_source" : {
          "firstName" : "George",
          "lastName" : "Ouma",
          "pageUri" : "http://www.wikipedia.com/story/683732/england-football-team-escaped-terrorist-attack-at-1998-world-cup",
          "date" : "2019-05-16T12:29:08.1308811Z"
        }
      }
    ]

我的桶应该是这样的:

"buckets" : [
        {
          "key" : "www.espnfc.com",
          "doc_count" : 1
        },
        {
          "key" : "www.wikipedia.com",
          "doc_count" : 2
        }
      ]

我有以下关于我如何进行聚合的代码 sn-p,但是这个聚合基于完整的 url 而不是域名

var searchResponse = client.Search<Person>(s =>
    s.Size(0)

    .Query(q => q
        .MatchAll()
    )
    .Aggregations(a => a
        .Terms("visited_pages", ta => ta
            .Field(f => f.PageUri.Suffix("keyword"))
        )
    )
);

var aggregations = searchResponse.Aggregations.Terms("visited_pages");

任何帮助将不胜感激:)

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    我使用了下面的Terms Aggregation using Script

    请注意,查看您的数据,我想出了字符串逻辑。对它进行测试并根据您要查找的内容修改逻辑。

    最好的方法是尝试使用一个名为 hostname 的单独字段,其中包含您要查找的值并在其上应用聚合。

    但是,如果您遇到困难,我想下面的聚合可以提供帮助!

    聚合查询:

    POST <your_index_name>/_search
    {
      "size": 0,
      "aggs": {
        "my_unique_urls": {
          "terms": {
            "script" : {
              "inline": """
                String st = doc['pageUri.keyword'].value;
                if(st==null){
                  return "";
                } else {
                  return st.substring(0, st.lastIndexOf(".")+4);
                }
              """,
              "lang": "painless"
            }
          }
        }
      }
    }
    

    下面是我的回复:

    查询响应:

    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "my_unique_urls": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "http://www.espnfc.com",
              "doc_count": 1
            },
            {
              "key": "http://www.wikipedia.org",
              "doc_count": 1
            },
            {
              "key": "https://en.wikipedia.org",
              "doc_count": 1
            }
          ]
        }
      }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 非常感谢@kamal。这确实可以在控制台上使用。您知道如何在 Nest 上编写相同的脚本吗?否则,如果没有 Nest,这将是公认的答案。
    • 抱歉@georgeouma,我不是 C# 开发人员,但此链接应该对您有所帮助elastic.co/guide/en/elasticsearch/client/net-api/current/…
    • 您可以复制上述Script = new InlineScript("&lt;the_above_script&gt;")这一行中提到的脚本,看看是否可行。希望有帮助!
    • 谢谢一群朋友
    【解决方案2】:

    我建议在摄取期间将该数据拆分到另一个字段(类似于“topleveldomain”),否则 Elasticsearch 必须为每个文档做大量工作才能进行聚合。

    【讨论】:

    • 非常感谢@joe-zack 抽出宝贵时间回复。这是个好建议。但是,我正在汇总包含数百万个文档的现有索引。您认为处理这种情况的最佳方法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2020-07-20
    • 1970-01-01
    相关资源
    最近更新 更多