How to make Examine in Umbraco v8 order results using one string field, or another if null/empty?
This should be dead simple, so I'll probably kick myself for not being to work it out later, but I'm having real trouble trying to get my examine search query to orderBy() multiple fields - help would be greatly appreciated!
What I'd like to happen is the query to order by one string field, but if that field is null or "", to use the other string, making one ordered list - e.g. a, a, b, b, c, d.
Instead, what seems to be happening is the query is ordering by one field then and ordering it all again by the other - e.g. a, b, c, d, a, b (but the final a, b are last because they are using the other string)
This is what I have so far:
SortableField altNameAZ = new SortableField("altName", SortType.String);
SortableField nodeNameAZ = new SortableField("nodeName", SortType.String);
tagQuery.OrderBy(nodeNameAZ, altNameAZ);
I've made both searchable in my FieldDefinitionsConfig.cs (so they do work singularly with orderBy())
To shed more light on the issue, I've been able to find plenty of detail on sorting Examine search results with multiple values, by chaining .OrderBy()'s (see https://github.com/Shazwazza/Examine/issues/97)
But that results in variables being treated differently e.g. a1, b1, c1, a2, b2. (the behaviour I am currently experiencing)
To clarify, what I need is a way of sorting the query with variables being treated equally - a1, a2, b1, b2, c1 and only using the second variable if the first is null or empty.
If it were with a normal list and a custom object, I would probably make a third naming variable, something like 'sortingName', which would be populated using 'altName' if not null, otherwise 'nodeName', then sorting by the new variable only, sidestepping the issue.
However, I can't work out how to do that with examine, since its pulling the data straight from the database, without making it into an editable object.
What definitely doesn't help matters is that there seems to be no official custom sorting documentation...
Hi James
Have you thought about adding a custom field, let’s call it field c, to the index which has the value from field a if it’s not empty and if it is then give it the value from field b. That way you can just sort results by field c.
Do you mean adding a custom field in an Umbraco doctype and then changing its value within the MVC structure before the examine query (to use values from a/b)? Or is there a way of adding a field directly to the index outside the CMS?
How to make Examine in Umbraco v8 order results using one string field, or another if null/empty?
This should be dead simple, so I'll probably kick myself for not being to work it out later, but I'm having real trouble trying to get my examine search query to orderBy() multiple fields - help would be greatly appreciated!
What I'd like to happen is the query to order by one string field, but if that field is null or "", to use the other string, making one ordered list - e.g. a, a, b, b, c, d.
Instead, what seems to be happening is the query is ordering by one field then and ordering it all again by the other - e.g. a, b, c, d, a, b (but the final a, b are last because they are using the other string)
This is what I have so far:
I've made both searchable in my FieldDefinitionsConfig.cs (so they do work singularly with orderBy())
Any ideas on what I'm doing wrong? (Thanks!)
Not had any luck with this since yesterday.
To shed more light on the issue, I've been able to find plenty of detail on sorting Examine search results with multiple values, by chaining .OrderBy()'s (see https://github.com/Shazwazza/Examine/issues/97)
But that results in variables being treated differently e.g. a1, b1, c1, a2, b2. (the behaviour I am currently experiencing)
To clarify, what I need is a way of sorting the query with variables being treated equally - a1, a2, b1, b2, c1 and only using the second variable if the first is null or empty.
If it were with a normal list and a custom object, I would probably make a third naming variable, something like 'sortingName', which would be populated using 'altName' if not null, otherwise 'nodeName', then sorting by the new variable only, sidestepping the issue.
However, I can't work out how to do that with examine, since its pulling the data straight from the database, without making it into an editable object.
What definitely doesn't help matters is that there seems to be no official custom sorting documentation...
https://github.com/Shazwazza/Examine/issues/97
Have I put this in the wrong forum?
I'm beginning to get the feeling it should be in https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/ instead - can anybody move the thread over, or do I have to manually open a new one?
Hi James Have you thought about adding a custom field, let’s call it field c, to the index which has the value from field a if it’s not empty and if it is then give it the value from field b. That way you can just sort results by field c.
Hi Paul,
Do you mean adding a custom field in an Umbraco doctype and then changing its value within the MVC structure before the examine query (to use values from a/b)? Or is there a way of adding a field directly to the index outside the CMS?
I meaning adding a custom field to the index like in this post. https://justnik.me/blog/indexing-sort-able-dates-in-umbraco-version-8
Took me a little to get my head around, but that article was exactly what I was looking for, thanks!
is working on a reply...