2

I'm having problem to reproduce search result in OpenSearch / ElasticSearch from DSL query to .NET C# code base.

Index was populated from .NET Core application based on this model:

public class Car
    {
        public string Producer { get; set; }
        public string Model { get; set; }
        public string Vin { get; set; }
        public ColorEnum Color { get; set; }
        public TransmissionType Transmission { get; set; }
        public CategoryType Category { get; set; }
    }

Index mapping for model above looks like this:


"mappings": {
  "_doc": {
    "properties": {
      "transmission": {
       "type": "long"
      },
      "color": {
       "type": "long"
      },
      "producer": {
       "type": "text",
       "fields": {
       "keyword": {
        "ignore_above": 256,
        "type": "keyword"
        }
       }
      },
     "model": {
      "type": "text",
      "fields": {
       "keyword": {
        "ignore_above": 256,
        "type": "keyword"
       }
      }
     },
     "vin": {
      "type": "text",
      "fields": {
       "keyword": {
        "ignore_above": 256,
        "type": "keyword"
        }
       }
      },
      "category": {
       "type": "long"
       }
      }
     }

Below is working as expected DSL query

{
              "query": {
                "bool": {
                  "must": [
                    { "term": { "color": 0 }},
                    { "term": { "transmission": 0 }},
                    { "term": { "producer": "volvo" } }
                    ]}}
}

The C# query that uses OpenSearch or ElasticSearch .NET Client and should produce that same outcome as DSL query is not respecting all required terms.

            var documents = await _openSearchClient.SearchAsync<Car>(s => s
                .Index(_indexName)
                .Query(q => q
                    .Bool(b => b
                        .Must(m => m.Term("category", 0))
                        .Must(m => m.Term("transmission", 0))
                        .Must(m => m.Term("producer", producer)))));

             return documents?.Documents;

The outcome of C# API call contain also documents that have category values set to 1 and transmission set to 1. It looks like only last term with producer name is respected.

Do i made mistake in c# query or there is something how to queries are executed ?

Thanks for all the help.

1

1 Answer 1

1

I found a solution based on input from this post

  var documents = await _openSearchClient.SearchAsync<Car>(s => s
            .Index(_indexName)
            .Query(q => q
                .Bool(bq => bq
                    .Must(mq => mq
                      .Term("transmission", 0) 
                                && mq.Term("color",0) 
                                && mq.Term("producer",producer)))));   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.