NEST返回null而不是字段 [英] NEST returns null instead of fields

查看:62
本文介绍了NEST返回null而不是字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Elastic已有很长时间了,但是我从未编写过获取某些数据的代码.现在我有麻烦了.

I'm working with elastic for a long time, but I have never written a code that fetches some data. And now I'm in trouble.

我有一个索引,我想在其中检索一些投影在某个字段上的文档.我真的可以用SQL编写它

I have an index where I want to retrieve some documents projected on some field. I could literally write it in SQL

SELECT myDocumentField
FROM myIndex

但是由于某些原因,我得到的是空值而不是值.

But for some reason I'm getting nulls instead of values.

索引中有6个文档.所以我写了以下查询:

I have 6 documents in my index. So I write following query:

var elasticServiceNumbers = await _elasticClient.SearchAsync<ElasticRequest>(
                                s => s.Query(Selector));

它可以正常工作并返回这6个值,除了它们的所有字段均为空

It works as expected and return these 6 values, except that all their fields are nulls

好的,我也在尝试添加字段:

Okay, I'm trying to add fields as well:

var elasticServiceNumbers = await _elasticClient.SearchAsync<ElasticRequest>(
                                s => s.StoredFields(sf => sf.Fields(f => f.ServiceNumber))
                                      .Query(Selector));
var elasticServiceNumbers2 = await _elasticClient.SearchAsync<ElasticRequest>(
                                s => s.Source(sf => sf.Includes(fds => fds.Field(f => f.ServiceNumber)))
                                      .Query(Selector));

但是仍然不走运,并且字段保留其空值.

But still out of luck, and fields keep their null values.

Kibana显示该字段存在于索引中:

Kibana shows that this fields exist on the index:

这可能是什么问题?

Kibana查询

{
  "version": true,
  "size": 500,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "_source": {
    "excludes": []
  },
  "aggs": {
    "2": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "3h",
        "time_zone": "Asia/Baghdad",
        "min_doc_count": 1
      }
    }
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [
    "@timestamp",
    "fields.Date",
    "fields.DeserializedMessage.Message.Date",
    "fields.DeserializedMessage.Message.Periods.Begin",
    "fields.DeserializedMessage.Message.Periods.End",
    "fields.DeserializedMessage.Message.ResponseDate",
    "fields.Periods.Begin",
    "fields.Periods.End",
    "fields.ResponseDate"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        },
        {
          "range": {
            "@timestamp": {
              "gte": 1527973200000,
              "lte": 1528577999999,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  },
  "highlight": {
    "pre_tags": [
      "@kibana-highlighted-field@"
    ],
    "post_tags": [
      "@/kibana-highlighted-field@"
    ],
    "fields": {
      "*": {}
    },
    "fragment_size": 2147483647
  }
}

推荐答案

看起来有问题的字段是

It looks like the fields in question are doc value fields , in that they are searchable and aggregatable but are not stored i.e. the original _source document sent to Elasticsearch is not stored.

使用NEST获取文档值字段

To get the doc value fields with NEST

var searchResponse = client.Search<ElasticRequest>(s => s
    .DocValueFields(f => f
        .Field(ff => ff.ServiceNumber.Suffix("keyword"))
    )
);

这使用了 serviceNumber keyword 映射,该映射看起来是doc值字段.

This is using the keyword mapping of serviceNumber which looks to be a doc value field.

这篇关于NEST返回null而不是字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆