Elasticsearch.net help with mapping - C#

Hey there.

I've a problem when i'm trying to mapping the following DSL query, in Visual Studio using Elasticsearch.net

{
"_source": {
"includes": [
"Vrvirksomhed.cvrNummer",
"Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn",
"Vrvirksomhed.virksomhedMetadata.nyesteBeliggenhedsadresse"
]
},
"query":{
  "multi_match" : {
     "query": "a",
     "fields": [ "Vrvirksomhed.cvrNummer", "Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn" ]
   }
  },
  "size":2
}

Here are its results:

"hits": {
    "total": 30018,
    "max_score": 7.6565695,
    "hits": [
        {
            "_index": "cvr-v-20181028",
            "_type": "_doc",
            "_id": "4001731906",
            "_score": 7.6565695,
            "_source": {
                "Vrvirksomhed": {
                    "cvrNummer": 31903394,
                    "virksomhedMetadata": {
                        "nyesteNavn": {
                            "navn": "A//A"
                        },
                        "nyesteBeliggenhedsadresse": {
                            "vejkode": 6378,
                            "fritekst": null,
                            "adresseId": "0a3f509d-13a3-32b8-e044-0003ba298018",
                            "vejnavn": "Sigynsgade",
                            "bogstavTil": null,
                            "conavn": "Anders Andersen",
                            "bogstavFra": null,
                            "periode": {
                                "gyldigFra": "2016-02-02",
                                "gyldigTil": "2019-03-31"
                            },
                            "kommune": {
                                "sidstOpdateret": "2006-11-13T00:00:00.000+01:00",
                                "kommuneNavn": "KØBENHAVN",
                                "kommuneKode": 101,
                                "periode": {
                                    "gyldigFra": "2007-01-01",
                                    "gyldigTil": null
                                }
                            },
                            "husnummerFra": 41,
                            "postboks": null,
                            "sidstOpdateret": "2019-03-31T15:10:25.000+02:00",
                            "postnummer": 2200,
                            "etage": "4",
                            "bynavn": null,
                            "husnummerTil": null,
                            "sidedoer": "th",
                            "landekode": "DK",
                            "sidstValideret": "2017-09-16T13:13:58.703+02:00",
                            "postdistrikt": "København N"
                        }
                    }
                }
            }
        }

Here is my code in C#:

Mapping classes

public class Vrvirksomhed
{
    public string cvrNummer { get; set; }
    public VirksomhedMetadata virksomhedMetadata { get; set; }
}

public class VirksomhedMetadata
{
    public NyesteNavn nyesteNavn { get; set; }
    public NyesteBeliggenhedsadresse nyesteBeliggenhedsadresse { get; set; }
}

public class NyesteNavn
{
    public string navn { get; set; }
}

and so on...

Client response script:

var searchResponse1 = client.Search<Vrvirksomhed>(s => s
            .Source(src => src
                .Includes(i => i
                    .Fields(
                        f => f.cvrNummer,
                        f => f.virksomhedMetadata.nyesteNavn.navn,
                        f => f.virksomhedMetadata.nyesteBeliggenhedsadresse
                     )
                )
            )
            .Query(q => q
                .MultiMatch(mm => mm
                    .Fields(mf => mf
                        .Field(mv => mv.cvrNummer)
                        .Field(mv => mv.virksomhedMetadata.nyesteNavn.navn)
                    )
                    .Query(searchInput)
                )
            )
            .Size(takeItems)
        );

It returns 0 hits, but when i change the type to dynamic it gets the hits as expected.

        var searchResponse2 = client.Search<dynamic>(s => s
            .Source(src => src
                .Includes(i => i
                      .Fields(
                        "Vrvirksomhed.cvrNummer",
                        "Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn",
                        "Vrvirksomhed.virksomhedMetadata.nyesteBeliggenhedsadresse"
                     )
                )
            )
            .Query(q => q
                .MultiMatch(mm => mm
                    .Fields(mf => mf
                        .Field("Vrvirksomhed.cvrNummer")
                        .Field("Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn")
                    )
                    .Query(searchInput)
                )
            )
            .Size(takeItems)
        );

Any idea what i'm doing wrong here?

Thanks for your help.

If you capture the request to Elasticsearch, do the query JSON and the API endpoint match exactly what you expect, and are the same as in the case where you use dynamic?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.