【问题标题】:NEST 7: How to get occurrence number for each document?NEST 7:如何获取每个文档的出现次数?
【发布时间】:2020-06-27 07:35:53
【问题描述】:

我正在使用 NEST 7.0 和 C# 运行使用 Fluent DSL 样式的 Elasticsearch 存储搜索查询。我使用 MultiMatch 在多个字段中通过传递的字符串值进行搜索:

queryContainer &= Query<Document>.MultiMatch(m => m.Fields(fields)
                                                   .Query(searchParams.SearchValue)
                                                   .Type(TextQueryType.MostFields));

对于每个文档,我收到它的 _score 和源数据。我都可以从 Response.Hits 中获得。

但是如何获取每个文档的搜索值的出现次数?我想收到这样的东西:

搜索值:“搜索”
搜索字段:标题、描述
结果:
- Doc1:5 次出现
- Doc2:0 次出现
- Doc3:出现 3 次
- Doc4:1 次出现
...

提前感谢您的帮助!

【问题讨论】:

  • 抱歉,您是指返回的每个文档中的word you've searched 中的count 吗?
  • @OpsterESNinja-Kamal,是的,你是对的

标签: .net elasticsearch nest elasticsearch-net


【解决方案1】:

在弹性搜索中没有直接的方法。 可以做的最接近的事情是使用multi-term vectors

查询

POST /index51/_mtermvectors
{
    "ids" : ["1", "2"], --> Ids of all documents (_id)
    "parameters": {
        "fields": [
            "text"
        ],
        "term_statistics": true
    }
}

它将返回所有文档的列表以及字段中每个单词的统计信息

结果:

{
  "docs" : [
    {
      "_index" : "index51",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "found" : true,
      "took" : 3,
      "term_vectors" : {
        "text" : {
          "field_statistics" : {
            "sum_doc_freq" : 7,
            "doc_count" : 3,
            "sum_ttf" : 7
          },
          "terms" : {
            "another" : {
              "doc_freq" : 2,
              "ttf" : 2,
              "term_freq" : 1,
              "tokens" : [
                {
                  "position" : 0,
                  "start_offset" : 0,
                  "end_offset" : 7
                }
              ]
            },
            "test" : {
              "doc_freq" : 3,
              "ttf" : 3,
              "term_freq" : 1,
              "tokens" : [
                {
                  "position" : 2,
                  "start_offset" : 16,
                  "end_offset" : 20
                }
              ]
            },
            "twitter" : {
              "doc_freq" : 2,
              "ttf" : 2,
              "term_freq" : 1,
              "tokens" : [
                {
                  "position" : 1,
                  "start_offset" : 8,
                  "end_offset" : 15
                }
              ]
            }
          }
        }
      }
    },
    {
      "_index" : "index51",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 1,
      "found" : true,
      "took" : 2,
      "term_vectors" : {
        "text" : {
          "field_statistics" : {
            "sum_doc_freq" : 7,
            "doc_count" : 3,
            "sum_ttf" : 7
          },
          "terms" : {
            "test" : {
              "doc_freq" : 3,
              "ttf" : 3,
              "term_freq" : 1,
              "tokens" : [
                {
                  "position" : 0,
                  "start_offset" : 0,
                  "end_offset" : 4
                }
              ]
            }
          }
        }
      }
    }
  ]
}

可以使用scroll api获取所有文档的ID

【讨论】:

    猜你喜欢
    • 2010-12-27
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多