简体   繁体   中英

C# elastic search query, match multiple queries

I have 2 elastic search queries that need to be matched by each fetched document in different ways. The "pools" query is a terms query. Each document has a list of pools attached to it, each of them being a string, and at least one of those pools must be in the provided list of pools in the "pools" query.

The other query is actually composed of multiple queries and at least 75% percent of them should be matched.

So in order for a document to be matched, the "pools" query must always be matched and from the other query, at least 75% must be matched.

I wrote my query like this:

 var matchQuery = BuildQuery(searchCriteria);
 var poolQuery = BuildPoolsQueryField(searchCriteria);

 // prepare the data for elasticsearch
 var result = await _elasticSearchClient.SearchAsync<ElasticPersonEntity>(
          p => p.Query(q => q
                 .Bool(b => b.Must(poolQuery).Should(matchQuery.ToArray())
                 .MinimumShouldMatch(MinimumShouldMatch.Percentage(75))))).ConfigureAwait(false);

But I could not find anywhere on the internet if you can chain multiple Should and Must clauses and what it happens if you chain them like this.

According to your description, your query is wrong: you need to mustpoolQuery && matchQuery(75%) so

The .MinimumShouldMatch(MinimumShouldMatch.Percentage(75) should be inside your matchQuery :

I join an example (using my data, but this should solve your problem)

.Query(q => q
                    .Bool(b => b
                        .Must(
                            mu => mu.Term(te => te.CntCd, "FR"),
                            mu => mu.Bool(bo => bo
                                .Should(your should query).MinimumShouldMatch(75)    
                            )
                       )
                    )
                )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM