Searching multiple words in multiple fields using C# NEST

Hello All,

I want to write an elasticsearch query in C# which can search multiple words in multiple fields.
For example.
Query: John California Honda
Fields: Name, State, Car
This query should search the term John in all three fields and so on for the other terms present in the query.
Note: the sequence can differ and the number of terms in the query can be more or less.

var searchResult= client.Search<cars>(s => s
          .AllTypes()
            .Size(50)
            .Query(q => q.Bool(b => b
               .Must(mu => mu
                           .MultiMatch(m => m
                                   .Fields(f => f.Field("Name").Field("State").Field("Car"))
                             .Query("John")
                              ) && q
                            .MultiMatch(m => m
                        .Fields(f => f.Field("Name").Field("State").Field("Car"))
                               .Query("Califronia")
                            ) 

Above is my code, How can I modify this query that it should match according to the number of terms in the query string. (Dynamic)

1 Like

With multi_match query, the query input for each field will be analyzed using the analyzer configured for each field, so there should be no need to tokenize the input yourself when constructing a query. For example

var searchResult = client.Search<cars>(s => s
    .AllTypes()
    .Size(50)
    .Query(q => q
        .MultiMatch(m => m
            .Fields(f => f
                .Field("Name")
                .Field("State")
                .Field("Car")
            )
            .Query("John California Honda")
        )
    )
);

is equivalent to

var searchResult = client.Search<cars>(s => s
    .AllTypes()
    .Size(50)
    .Query(q => q
        .DisMax(dm => dm
            .Queries(dq => dq
                .Match(m => m
                    .Field("Name")
                    .Query("John California Honda")
                ), dq => dq
                .Match(m => m
                    .Field("State")
                    .Query("John California Honda")
                ), dq => dq
                .Match(m => m
                    .Field("Car")
                    .Query("John California Honda")
                )
            )
        )

    )
);

where the query input to each match query will be analyzed using the analyzer configured for search for each of the "Name", "State" and "Car" fields, respectively.

2 Likes

Thank you for taking the time to reply.
Can you also guide me which analyzer works best with numeric fields?

You don't set an analyzer for numeric fields, only for text datatype fields.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.