2

I am trying to query using matchphrase but on multiple fields but my nest allows me to do it on only one field here's my code snippet

    var result = client.Search<document>(s => s
    .Analyzer("automplete")
    .Query(p => p
    .MatchPhrase(M => M
    .OnField("description")
    .Query(value))));

I have multiple fields in class documents and I want search on that fields too.

Please help me with this - thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pavan sp
  • 105
  • 2
  • 17

1 Answers1

4

match_phrase does not even support multiple fields. For using a match query over multiple fields, you need to use multi_match query.

var result = client.Search<document>(s => s
    .Analyzer("automplete")
    .Query(p => p
        .MultiMatch(m => m
            .OnFields(new[] { "description" /*, add other fields here */ })
            .Query(value)
            .Type(TextQueryType.Phrase))));
bittusarkar
  • 6,247
  • 3
  • 30
  • 50
  • @bittusarkar: thanks for the answer.. It helped me a lot.. And one more question. How can I initialize `var result` before hand just like we do `string result = string.empty`. Can I do the same for elasticsearch ?? TIA :) – ASN May 23 '16 at 06:36
  • @ASN I did not understand your question. What would you want to do that anyway? You can always initialize it with `null`. – bittusarkar May 23 '16 at 07:56
  • How can I initialize var response before hand just like we do `string result = string.empty`. Can I do the same for elasticsearch ?? Generally we use `var response= client.search(//some code here);` Instead of that i want to initialize `response` first and then use the parameter later in the code. I hope you understood now or else I can mail you if you can share your mail id. – ASN May 23 '16 at 08:00
  • I want to write a if else condition which takes two different queries, but the processing of result is same after the response is obtained. so wanted to initialize it before if-else loop. I tried initializing it with null and it threw error as we are equating it with search response after initialization. So I initialized using `var response = (ISearchResponse)null;`. This served the purpose. Thanks. Your answer gave me an idea. :) – ASN May 23 '16 at 08:53
  • Oh, don't use `var` in this case. A simple `ISearchResponse response = null;` should be good enough :) – bittusarkar May 23 '16 at 10:17