1

I have an class called Product, which can be available in 1 or more countries (but rarely all of them).

I want to include in my query something that makes the my search/query only return products that is sold in that country.

My best bet would be to have a list in my product object, with CountryIds (Ids of countries that the product is sold in). But I cannot find a query parameter in NEST to find all products whose "CountryIds" list contain the given ID (which I know at runtime). Basically I would like some help on how to write the contents of this method through elastic search.

Product[] GetProductsByCountryId(int countryId);

The Product class could look something like this:

class Product {
   string productName;
   int[] countryIds;
}

2 Answers 2

1

The term query will return documents that contain a country ID within the countryIDs.

If you have multiple country IDs, the terms query will return documents that contain at least one of the country IDs in the countryIDs field.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I find the documentation quite confusing, but the terms query worked for me.
0

Russ Cam gave me the right idea, but I just wanted to post my full code result:

_client.Search<Product>(s =>
            s.Query(q => q.FunctionScore(fs => fs.Functions(ff =>
                ff.ScriptScore(
                    ss =>
                        ss.Script(script => script.Params(p => PopulateParameterDictionary(p))
                            .Inline(GenerateSearchAlgorithm())
                        )))) 
                    &&+  q.Terms(c => c.Name("country_ids_query")
                         .Field(f => f.CountryIdsWhereAvailable)
                         .Terms(country.Id))));

This result includes a scoring with an inline script as well. But the important part of the answer here is the second query "country_ids_query".

This filters away products whose CountryIdsWhereAvailable integer array does not contain "country.Id", and still scores them based on my script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.