Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • George Kent-Nye 26 posts 172 karma points
    Apr 28, 2021 @ 18:13
    George Kent-Nye
    0

    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())

    umbIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("nodeName", FieldDefinitionTypes.FullTextSortable));
    
    umbIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("altName", FieldDefinitionTypes.FullTextSortable));
    

    Any ideas on what I'm doing wrong? (Thanks!)

  • George Kent-Nye 26 posts 172 karma points
    Apr 29, 2021 @ 16:42
    George Kent-Nye
    0

    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

  • George Kent-Nye 26 posts 172 karma points
    May 05, 2021 @ 11:57
    George Kent-Nye
    0

    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?

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    May 05, 2021 @ 13:17
    Paul Seal
    0

    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.

  • George Kent-Nye 26 posts 172 karma points
    May 05, 2021 @ 16:44
    George Kent-Nye
    0

    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?

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    May 05, 2021 @ 16:53
    Paul Seal
    101

    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

  • George Kent-Nye 26 posts 172 karma points
    May 06, 2021 @ 13:21
    George Kent-Nye
    0

    Took me a little to get my head around, but that article was exactly what I was looking for, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft