0

I have this C# code which is expected to match 2 fields using multi-match Elastic Search type. I am using NEST package.

var response = await _elasticClient.SearchAsync<FileDocument>(
     s => s.Query(q => q.MultiMatch(c => c
     .Fields(f => f.Field(p => p.FileName).Field(query))
     .Fields(f => f.Field(p => p.Metadata).Field(query))
     )));

Problem is no matter what text I passed in, it returns all the result. Anything I miss out?

Steve
  • 2,963
  • 15
  • 61
  • 133

2 Answers2

1

In order to efficiently debug these types of issue, you need to inspect the HTTP request going to Elasticsearch, ultimately your query builder will be converted to search JSON and will be executed against Elasticsearch.

I am not aware of nest but have written the answer for Java code, which prints the Elasticsearch query in JSON format.

Although my guess is that, you are not sending the correct HTTP method which should be POST and you might be sending it with GET, which is causing ES to ignore your search query and return all documents.

Amit
  • 30,756
  • 6
  • 57
  • 88
1

Solved after adding .Query(query)

var response = await _elasticClient.SearchAsync<FileDocument>(
                 s => s.Query(q => q.MultiMatch(c => c
                 .Fields(f => f.Field(p => p.FileName).Field(p=>p.Metadata))
                 .Query(query)
                 ))
                 );

Reference - https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/multi-match-usage.html

Steve
  • 2,963
  • 15
  • 61
  • 133
  • 1
    you can mark it answer, after sometime so that it would be helpful for community, also any pointer how you debugged and found that its missing :) – Amit Sep 30 '20 at 03:17