Skip to content

"DataMember" attribute is not applied when deserializing searching query #3107

Closed
@QuanTran91

Description

@QuanTran91

NEST/Elasticsearch.Net version: 6.0.1

Elasticsearch version: 6.0.1

Description of the problem including expected versus actual behavior:

I have a document model as below:

[DataContract(Name = "source_entity")]
public class SourceEntity
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
    
    [DataMember(Name = "display_name")]
    public string DisplayName { get; set; }
}

And here is the code for inserting random source entities:

public class AddEntitySample
{
    private ElasticClient elasticClient;

    public AddEntitySample()
    {
        var node = new Uri("http://localhost:9200");
        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
        var settings = new ConnectionSettings(pool);
        
        settings.DefaultMappingFor<SourceEntity>(t => t.IndexName("source_entity_fun"));
        settings.DisableDirectStreaming();
        settings.OnRequestCompleted((t) =>
        {
            var a = t.DebugInformation;
        });
        elasticClient = new ElasticClient(settings);
    }

    public void AddSampleData()
    {
        elasticClient.DeleteIndex(Indices.Index<SourceEntity>());
        var enumberation = Enumerable.Range(0, 1000).Select(index => new SourceEntity
        {
            Name = "Name " + index,
            DisplayName = "DisplayName" + index
        });        
        var response = elasticClient.BulkAsync(t => t.IndexMany(enumberation), 
            new System.Threading.CancellationToken()).Result;
    }        
}

After insert some items in elastic db, I use this command "GET source_entity_fun/_mapping" (in Kibana dev console) to get the schemas of SourceEntity in elastic db.
And here is the json format of the document

{
  "source_entity_test": {
    "mappings": {
      "sourceentity": {
        "properties": {
          "display_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

When I make a simple query:

var query = elasticClient.Search<SourceEntity>(t => t.Query(u => u.Terms(k => k.Field(h => h.DisplayName).Terms("23"))));

And here is the json query generated (using DebugInformation):

{"query":{"terms":{"displayName":["23"]}}}

As the result, there are no documents returned.
I would expected is should be :

{"query":{"terms":{"display_name":["23"]}}}

(with underscore)

I tried to use NEST.JsonNetSerializer but it did not work.
Could you help me, please?
Thanks.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions