2
votes

I am writing a query in nest for elasticsearch that matches to a list of countries - it cutrrently matches whenever any of the countries in the list is present in ESCountryDescription (a list of countries). I only want to match when all of the countries in CountryList match ESCountryDescription. I believe that I need to use MinimumShouldMatch as in this example http://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl-terms-query.html

a.Terms(t => t.ESCountryDescription, CountryList)

But I cannot find a way of adding MinimumShouldMatch into my query above.

2

2 Answers

2
votes

You can apply MinimumShouldMatch patameter in TermsDescriptor. Here is an example:

var lookingFor = new List<string> { "netherlands", "poland" };

var searchResponse = client.Search<IndexElement>(s => s
    .Query(q => q
        .TermsDescriptor(t => t.OnField(f => f.Countries).MinimumShouldMatch("100%").Terms(lookingFor))));

or

var lookingFor = new List<string> { "netherlands", "poland" };

var searchResponse = client.Search<IndexElement>(s => s
                .Query(q => q
                    .TermsDescriptor(t => t.OnField(f => f.Countries).MinimumShouldMatch(lookingFor.Count).Terms(lookingFor))));

And this is the whole example

class Program
{
    public class IndexElement
    {
        public int Id { get; set; }
        [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
        public List<string> Countries { get; set; }
    }

    static void Main(string[] args)
    {
        var indexName = "sampleindex";

        var uri = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(uri).SetDefaultIndex(indexName).EnableTrace(true);
        var client = new ElasticClient(settings);

        client.DeleteIndex(indexName);

        client.CreateIndex(
            descriptor =>
                descriptor.Index(indexName)
                    .AddMapping<IndexElement>(
                        m => m.MapFromAttributes()));

        client.Index(new IndexElement {Id = 1, Countries = new List<string> {"poland", "germany", "france"}});
        client.Index(new IndexElement {Id = 2, Countries = new List<string> {"poland", "france"}});
        client.Index(new IndexElement {Id = 3, Countries = new List<string> {"netherlands"}});

        client.Refresh();

        var lookingFor = new List<string> { "germany" };

        var searchResponse = client.Search<IndexElement>(s => s
            .Query(q => q
                .TermsDescriptor(t => t.OnField(f => f.Countries).MinimumShouldMatch("100%").Terms(lookingFor))));
    }
}

Regarding your problem

  1. For terms: "netherlands" you will get document with Id 3
  2. For terms: "poland" and "france" you will get documents with Id 1 and 2
  3. For terms: "germany" you will get document with Id 1
  4. For terms: "poland", "france" and "germany" you will get document with Id 1

I hope this is your point.

1
votes

Instead of doing

.Query(q => q
    .Terms(t => t.ESCountryDescription, CountryList))

You can use the command below

.Query(q => q
    .TermsDescriptor(td => td
        .OnField(t => t.ESCountryDescription)
        .MinimumShouldMatch(x)
        .Terms(CountryList)))

See this for unit tests in elasticsearch-net Github repository.