Elasticsearch, how to make NEST map response to cl

2020-07-18 03:44发布

问题:

First of all, I am using NEST 5.5.0.

I have the following use of a remote 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")
     )
)
);

The mappings in the index (without a bunch of other properties besides cvrNummer) are as follows:

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

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

      }
    }
  },          
}
}
}
}
}

I also have the following class which the result is supposed to be mapped to:

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

Now, the search (searchResponse) holds the expected results (1 result), where the part concerning cvrNummer looks as follows:

"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": [
...

However, when i look in searchResponse.Documents, I have the correct type (Company), but the value of cvrNumber is null.

Any ideas what I'm doing wrong, since the value of cvrNummer is not mapped into cvrNumber on the instance of Company in searchResponse.Documents?

Thanks in advance for your input!

UPDATE

I tried the following without success, still got the expected result, but cvrNumber is still null (in 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; }
}

With the query:

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

UPDATE

It works with the following modifications to the query:

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

回答1:

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

Vrvirksomhed looks like it should be a POCO property on Company mapped either as an object datatype or nested datatype (take a look at nested objects in the Definitive Guide for the differences), where that POCO has a property called cvrNumber, similar to

[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; }
}