NEST 7:如何获取每个文档的出现编号?

问题描述 投票:0回答:1

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

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

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

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

搜索值:“搜索”搜索字段:标题,说明结果:-Doc1:5次-Doc2:出现0次-Doc3:3次-Doc4:发生1次...

谢谢您的帮助!

.net elasticsearch nest elasticsearch-net
1个回答
0
投票

在弹性搜索中没有直接的方法可以做到。可以做的最接近的事情是使用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 >>

© www.soinside.com 2019 - 2024. All rights reserved.