一尘不染

Elasticsearch,如何使NEST映射响应类

elasticsearch

首先,我正在使用NEST 5.5.0。

我对远程elasticsearch-index有以下用法:

var node = new Uri("http://distribution.virk.dk/cvr-permanent");

        var settings = new 
ConnectionSettings(node).DefaultIndex("virksomhed");

        settings.BasicAuthentication("username", "password");

        var client = new ElasticClient(settings);

        var searchResponse = client.Search<Company>(s => s
.AllTypes().Query(q => q
     .Match(m => m
        .Field(f => f.cvrNumber)
        .Query("35954716")
     )
)
);

索引中的映射(除了cvrNummer之外没有其他属性)如下:

{
"cvr-permanent-prod-20170205" : {
"mappings" : {
"virksomhed" : {
"_size" : {
  "enabled" : true
},
"properties" : {
  "Vrvirksomhed" : {
    "properties" : {

        "type" : "long"
      },
      "cvrNummer" : {
        "type" : "string"
      },

      }
    }
  },          
}
}
}
}
}

我还有下面的类,其结果应该映射到:

[ElasticsearchType(Name = "virksomhed")]
public class Company
{
    [Text(Name = "Vrvirksomhed.cvrNummer")]
    public string cvrNumber { get; set; }
}

现在,搜索(searchResponse)保留了预期的结果(1个结果),其中与cvrNummer有关的部分如下所示:

"hits": {
"total": 1,
"max_score": 17.34601,
"hits": [
  {
    "_index": "cvr-permanent-prod-20170205",
    "_type": "virksomhed",
    "_id": "4000333383",
    "_score": 17.34601,
    "_source": {
      "Vrvirksomhed": {
        "cvrNummer": 35954716,
        "regNummer": [
          {
            "regnummer": "A/S35855",
            "periode": {
              "gyldigFra": "1956-06-01",
              "gyldigTil": "1999-10-18"
            },
            "sidstOpdateret": "2015-02-10T00:00:00.000+01:00"
          }
        ],
        "brancheAnsvarskode": null,
        "reklamebeskyttet": false,
        "navne": [
...

但是,当我查找searchResponse.Documents时,我具有正确的类型(公司),但是cvrNumber的值为null。

任何想法我在做什么错,因为cvrNummer的值未映射到searchResponse.Documents中Company实例上的cvrNumber?

预先感谢您的输入!

更新

我尝试以下操作均未成功,但仍然获得了预期的结果,但是cvrNumber仍然为null(在searchResponse.Documents中):

[ElasticsearchType(Name = "virksomhed")]
public class Company
{
    [Object(Name = "Vrvirksomhed")]
    public Vrvirksomhed Vrvirksomhed { get; set; }
}

public class Vrvirksomhed
{
    [Text(Name = "cvrNummer")]
    public string cvrNumber { get; set; }
}

与查询:

 var searchResponse = client.Search<Vrvirksomhed>(s => s
            .AllTypes().Query(q => q
                .Match(m => m
                    .Field(f => f.cvrNumber)
                    .Query("35954716")
                )
            )
        );

更新

它对查询进行了以下修改:

 var searchResponse = client.Search<Company>(s => s
            .AllTypes().Query(q => q
                .Match(m => m
                    .Field(f => f.Vrvirksomhed.cvrNumber)
                    .Query("35954716")
                )
            )
        );

阅读 339

收藏
2020-06-22

共1个答案

一尘不染

[ElasticsearchType(Name = "virksomhed")]
public class Company
{
    [Text(Name = "Vrvirksomhed.cvrNummer")]
    public string cvrNumber { get; set; }
}

Vrvirksomhed看起来应该是Company映射为对象数据类型嵌套数据类型的POCO属性(请参阅《权威指南》中的嵌套对象以了解差异),其中POCO具有称为的属性cvrNumber,类似于

[ElasticsearchType(Name = "virksomhed")]
public class Company
{
    [Object(Name = "Vrvirksomhed")]
    public Vrvirksomhed Vrvirksomhed { get; set; }
}

public class Vrvirksomhed
{
    [Text(Name = "cvrNummer")]
    public string cvrNumber { get; set; }
}
2020-06-22