Comments (5)
Been covering this myself and been very reliant on the source code to guide. If it helps though I can share the approach we've been using to refactor existing code.
As far as I can make out the concept of ANDs and ORs you had in the NEST client has been remove - and I actually I think this may be a good thing because I don't think they exist conceptually in the search language.
So below is an example of how we refactored from using the operators to Action delegates so we didn't have to refactor existing if statements to much, same can be done with for statements and sorts and other things.
private static Action<QueryDescriptor<MySearchModel>> BuildQuery([NotNull] MyRequest request)
{
var musts = new List<Action<QueryDescriptor<MySearchModel>>>();
var mustNots = new List<Action<QueryDescriptor<MySearchModel>>>();
var shoulds = new List<Action<QueryDescriptor<MySearchModel>>>();
if (request.TagIds?.Any() ?? false)
{
musts
.Add(m => m
.Terms(t => t
.Field(f => f.Tags)
.Terms(new TermsQueryField(request.TagIds.Select(x => FieldValue.Long(x)).ToArray()))
)
);
}
if (request.DocumentTypeId != null && request.DocumentTypeId != 0)
{
musts
.Add(m => m
.Term(t => t
.Field("docTypeId")
.Value(FieldValue.Long(request.DocumentTypeId ?? 0))
)
);
}
mustNots
.Add(m => m
.Term(t => t
.Field(f => f.HideInSearchResults)
.Value(FieldValue.Boolean(true))
)
);
if (request.ExcludedDocumentTypeIds != null && (request.ExcludedDocumentTypeIds?.Any() ?? false))
{
mustNots
.Add(m => m
.Terms(t => t
.Field("docTypeId")
.Terms(new TermsQueryField(request.ExcludedDocumentTypeIds.Select(x => FieldValue.Long(x)).ToArray()))
)
);
}
if (request.ExcludedPageIds != null && (request.ExcludedPageIds?.Any() ?? false))
{
mustNots
.Add(m => m
.Terms(t => t
.Field("id")
.Terms(new TermsQueryField(request.ExcludedPageIds.Select(x => FieldValue.Long(x)).ToArray()))
)
);
}
if (request.PrimaryTagId.HasValue && request.PrimaryTagId != 0)
{
shoulds
.Add(s => s
.Term(t => t
.Field("primaryTagId")
.Value(FieldValue.Long(request.PrimaryTagId.Value))
)
);
}
return query => query
.Bool(b => b
.Must(musts.ToArray())
.MustNot(mustNots.ToArray())
.Should(shoulds.ToArray())
);
}
We can then refactor / create the search result descriptor as below:
Action<SearchRequestDescriptor<MySearchModel>> searchRequest = s => s
.Index("my-index")
.Query(BuildQuery(request))
.Sort(s => s.Field(f => f.UpdatedOn, fs => fs.Order(SortOrder.Desc)))
.From(0)
.Size(20);
Multiple sorting fields can be addressed in a similar manner:
private static Action<SortOptionsDescriptor<MySearchModel>>[] BuildSort(string sortOption)
{
return sortOption.ToLower() switch
{
"sort2" => new Action<SortOptionsDescriptor<MySearchModel>>[]
{
s => s.Field(f => f.MyField1, fs => fs.Order(SortOrder.Asc)),
s => s.Field(f => f.MyField2, fs => fs.Order(SortOrder.Desc)),
},
"sort3" => new Action<SortOptionsDescriptor<MySearchModel>>[]
{
s => s.Field(f => f.MyField3, fs => fs.Order(SortOrder.Desc)),
s => s.Score(new ScoreSort() { Order = SortOrder.Desc })
},
_ => new Action<SortOptionsDescriptor<MySearchModel>>[]
{
s => s.Field(f => f.MyField4, fs => fs.Order(SortOrder.Desc)),
s => s.Field(f => f.Dates.FirstOrDefault().StartDate, fs => fs.Order(SortOrder.Asc)),
s => s.Field(f => f.Dates.FirstOrDefault().EndDate, fs => fs.Order(SortOrder.Asc)),
},
};
}
Your Sort line would then become:
Action<SearchRequestDescriptor<MySearchModel>> searchRequest = s => s
.Index("my-index")
.Query(BuildQuery(request))
.Sort(BuildSort(sortOption))
.From(0)
.Size(20);
I can't vouch if this is 100% the approach that should be taken. The documentation is very thin on client usage, and doesn't seem to be up to date with the source code or NuGet packages. But it is working for us so far.
Based on using upgrading from NEST 7.17.5 to Elastic.Clients.Elasticsearch 8.13.12.
Hope people find this helpful.
from elasticsearch-net.
Hi @MarkMcDonald, Thanks so much for your reply. I understand that my current code does not have a solution in the new DLL, for me it is not viable make a migration because my project is very big and a have a lot of indexes with dynamic querys and nested querys... i'm really disapointed and indignant....
by the way, do you know something about my first question about Analysis templates?
thx again!!
from elasticsearch-net.
Related Issues (20)
- Support for (alternative for) GeoShapeQuery HOT 2
- After/AfterKey dictionary types in Composite Aggregation/Aggregate HOT 1
- 8.13.10 lacks UpdateMany<TSource, TPartialDocument>
- Latest MINOR package version does not support .Net 6 anymore. HOT 3
- Elastic.Transport.UnexpectedTransportException with Indices.GetAsync HOT 4
- Indices.ExistsAsync throw exception due to ICU field HOT 6
- Queries are no longer derive from SearchQuery HOT 4
- Make it easy to log elastic requests HOT 3
- DefaultMappingFor IndexName is not respected in searches HOT 3
- Different classes between filters and return document HOT 3
- can't created repository,response request body missing HOT 4
- Elasticsearch health is accessible but .net client fails HOT 3
- Client cannot deserialize string to list of strings HOT 4
- [Feature] Elastic 8 - MatchAll won't accept passing nothing HOT 1
- Creating a local stack s3 repository failed. use Postman works, but code calls are not working.
- [FEATURE] Elastic 8 Client - Stores Enums as Strings HOT 5
- Cannot use RawJsonQuery HOT 6
- Encountered an unsupported variant tag '' on 'Analysis.INormalizer', which could not be deserialized
- Simplify IndexAsync Abstraction to Infer Index Name from TDocument Type HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elasticsearch-net.