Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 394 posts 1477 karma points
    Feb 16, 2021 @ 12:11
    Paul de Quant
    0

    Searching in examine - one field multiple values

    Hi,

    I have a field in examine that I want to query against. This field typically has a comma separated value like this:

    ["Military","Graduates","Apprentices"]

    I want to be able to pass through another list of terms to see if there is a match i.e. return result if page/media contains "Graduates","Apprentices".

    Does anyone know how to do this? I suspect I'd use the GroupedOr, but I can only see how you pass through multiple fields but only a single value and not multiple values.

    Thanks

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Feb 16, 2021 @ 12:18
    Nik
    0

    Hey Paul,

    Okay so the first thing to note is that Examine doesn't like commas. So I would advise hooking into indexing events and updating the index value to replace comma's with spaces.

    (Note: Callum's Search Extensions package might do this for you, I've not played with it yet, but it looks awesome).

    As for searching, you would do a grouped or like this:

    .GroupedOr(new [] {"fieldAlias"}, myStringArrayOfValues)

    This should create you the correct lucene I believe.

    Cheers

    Nik

  • Paul de Quant 394 posts 1477 karma points
    Feb 16, 2021 @ 12:23
    Paul de Quant
    0

    Hi Nik,

    Thanks for the speedy response :)

    As I'm comparing two lists against each other, would this approach still work?

    If I changed the list from using commas to using spaces (so it looks like a sentence basically), what would the string array of values be? will it still work if the values are still comma separated?

    So in the index, this:

    ["Military","Graduates","Apprentices"]

    becomes:

    Military Graduates Apprentices

    But the values I pass through are still this: Military, Graduates

    Thanks

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Feb 16, 2021 @ 12:33
    Nik
    100

    So you have different options to be honest.

    What I do these days is the following.

    1. I keep my raw values as they index by default
    2. I create a new examine field with "cleaned" appended to the end (against which I do my searching)
    3. I then take advantage of Examines ability to store multiple values against a single field:

    Doing something a bit like this (my example is handling MNTP's)

    internal static void HandleMultiNodeTreePicker(IndexingItemEventArgs e, IPublishedContent content, string property, IUmbracoContextFactory umbracoContextFactory, ILogger logService)
        {
            try
            {
                var processed = new List<object>();
                if (content.HasProperty(property) && content.HasValue(property))
                {
                    IPublishedContent categoryNode;
    
                    using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
                    {
                        foreach (var entry in e.ValueSet.Values[property].Cast<string>())
                        {
                            foreach (var processedEntry in entry.Split(',').Select(s => GuidUdi.Parse(s)))
                            {
                                categoryNode = umbracoContextReference.UmbracoContext.Content.GetById(processedEntry.Guid);
                                if(categoryNode != null)
                                    processed.Add(categoryNode.Name.Replace(" ", ""));
                            }
                        }
                    }
    
                    e.ValueSet.Values.Add($"{property}Cleaned", processed);
                }
            }
            catch (Exception ex)
            {
                logService.Error(typeof(GenericIndexHelper), ex, $"Error Handle {property} - {content.Id}");
            }
        }
    

    In your case you'd code would be simpler as it would take a string, split it on ,'s to turn it into an array of string and index the array as individual items

    Then that search I suggested would do the following:

    if myFieldCleaned contains any of my search terms, return the result

    At least that is my understanding

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Feb 16, 2021 @ 12:37
    Nik
    0

    Also your search term would need to be like this:

    var searchTerm = new [] { "Graduate", "Apprentices" };
    

    Which will put it in the right format.

  • Paul de Quant 394 posts 1477 karma points
    Feb 16, 2021 @ 14:44
    Paul de Quant
    0

    Hi Nik,

    Where would this code go? Do I need to put it into a component?

    Thanks

  • Paul de Quant 394 posts 1477 karma points
    Feb 24, 2021 @ 08:48
    Paul de Quant
    0

    For anyone else reading this. I did indeed have to create a component to run this code.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies