Search code examples
elasticsearchnestelasticsearch-dsl

Simple query without a specified field searching in whole ElasticSearch index


Say we have an ElasticSearch instance and one index. I now want to search the whole index for documents that contain a specific value. It's relevant to the search for this query over multiple fields, so I don't want to specify every field to search in.

My attempt so far (using NEST) is the following:

var res2 = client.Search<ElasticCompanyModelDTO>(s => s.Index("cvr-permanent").AllTypes().
     Query(q => q
        .Bool(bo => bo
            .Must( sh => sh
               .Term(c=>c.Value(query))
            )
         )
     ));

However, the query above results in an empty query:

I get the following output, ### ES REQEUST ### {} , after applying the following debug on my connectionstring:

.DisableDirectStreaming()
.OnRequestCompleted(details =>
{
     Debug.WriteLine("### ES REQEUST ###");
     if (details.RequestBodyInBytes != null) Debug.WriteLine(Encoding.UTF8.GetString(details.RequestBodyInBytes));
            })
 .PrettyJson();

How do I do this? Why is my query wrong?


Solution

  • Try this

    var res2 = client.Search<ElasticCompanyModelDTO>(s => 
     s.Index("cvr-permanent").AllTypes()
    .Query(qry => qry
       .Bool(b => b
       .Must(m => m
           .QueryString(qs => qs
               .DefaultField("_all")
               .Query(query))))));