Copied to clipboard

Flag this post as spam?

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


  • Remko 104 posts 257 karma points
    Jul 16, 2019 @ 08:57
    Remko
    0

    Using RangeQuery on custom (string) fields (Umbraco 8)

    We used to add some rewritten values to the Examine Index to query it with Examine.

    For example a datetime picker with name "date" we used to save to index as "dateRewritten" with the correct format to order it and query with ranges like this: And().Range("dateRewritten", dateFrom, dateTo)

    Now in Umbraco 8 we managed to get the dateRewritten correctly in the index, with _TransformingIndexValues event.

    But when we now try to query on this field, and use .RangeQuery, we need to pass non nullable type, like long (we save ticks), but then an exception pops up: : 'Could not perform a range query on the field dateRewritten, it's value type is Examine.LuceneEngine.Indexing.FullTextType'

    Is there any way to do a RangeQuery on string field, like it was possible in U7?

  • Nik 1478 posts 6494 karma points MVP 4x c-trib
    Jul 16, 2019 @ 09:37
    Nik
    0

    Hey Remko,

    How are you manipulating your "dateRewritten" field?

    You will need to change it's type to a long in the index so you can do the range, this is the same as what was happening in v7 as you can't do range on text.

    Thanks

    Nik

  • Nik 1478 posts 6494 karma points MVP 4x c-trib
    Jul 16, 2019 @ 09:40
    Nik
    101

    Assuming you are using the External Index you can do the following:

    var externalIndex = examineManager.Indexes.FirstOrDefault(i=>i.Name == "ExternalIndex");
    if(externalIndex != null)
    {
          externalIndex.FieldDefinitionCollection.AddOrUpdate(new FiledDefinition("dateRewritten", FieldDefinitionTypes.Long));
    }
    

    This can then be used to perform a ranged query against once you've populated it with the ticks value for the given date.

    Nik

  • Mus'ab 155 posts 381 karma points notactivated
    Jun 09, 2020 @ 07:37
    Mus'ab
    0

    Hi Nik can you give a simple example to full code that searches using examine between tow dates? because I can't find .Indexes in ExamineManager and want to see in which way would I use the examine to make this code working well

  • Nik 1478 posts 6494 karma points MVP 4x c-trib
    Jun 09, 2020 @ 07:52
    Nik
    0

    Hi Mus'ab,

    Are you using Umbraco v7 or Umbraco v8?

    Cheers

    Nik

  • Mus'ab 155 posts 381 karma points notactivated
    Jun 09, 2020 @ 08:01
    Mus'ab
    0

    Hi Nik

    I am using v8

  • Nik 1478 posts 6494 karma points MVP 4x c-trib
    Jun 09, 2020 @ 08:14
    Nik
    0

    Have a read of my blog post about it: https://justnik.me/blog/indexing-sort-able-dates-in-umbraco-version-8

    It doesn't have an example of the searching side of it, but it's the indexing that is the bigger issue. I'll see if I dig out some example code of doing the search for you.

  • Mus'ab 155 posts 381 karma points notactivated
    Jun 09, 2020 @ 09:10
    Mus'ab
    0

    Hi Nik

    I have read this article before and tried to write that code in my project but I faced a problems with sorting indexes something like wasting the sort of index but let me know when you dig out some example code of doing the search

  • Alice Carter 1 post 71 karma points
    Mar 20, 2021 @ 11:25
    Alice Carter
    0

    ok this one was helpful actually, thanks

  • Remko 104 posts 257 karma points
    Jul 17, 2019 @ 13:11
    Remko
    1

    This was where I was looking for, thank you!

  • David Armitage 479 posts 2008 karma points
    Jun 08, 2020 @ 05:23
    David Armitage
    0

    Hi,

    I had the same issue with Umbraco 8.

    Once I added my custom field I had to add int rather than long.

    Here is My code to create the custom index set.

    e.ValueSet.Add(alias, value);
    e.Index.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition(alias, FieldDefinitionTypes.Integer));
    

    Here is my code for the range search.

    examineQuery.And().RangeQuery<int>(fields, 2, 10, true);
    

    Thinking about it now I am assuming I could change my range query to long and then Nik's example would probably work for me too. I haven't tested this yet through.

    examineQuery.And().RangeQuery<long>(fields, 2, 10, true);
    

    Hope this helps someone.

    Regards

    David

  • David Armitage 479 posts 2008 karma points
    Mar 20, 2021 @ 05:08
    David Armitage
    0

    Hi All,

    It might also be useful to post this here.

    Here is an example of an Examine date RangeQuery.

    Here is the ValueSet I added.

    e.ValueSet.Add("comparableDate", date);
    e.Index.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("comparableDate", FieldDefinitionTypes.DateTime));
    

    And here is the Examine query

    examineQuery.And().RangeQuery<DateTime>(fields, DateTime.MinValue, DateTime.Now);
    

    Hope this helps someone.

    Regards

    David

  • Mahdi Shahbazi 4 posts 76 karma points
    Oct 19, 2021 @ 13:14
    Mahdi Shahbazi
    1

    In umbraco 9 you can configure it as following:

    public sealed class ConfigureCustomIndexOptions : IPostConfigureOptions<LuceneDirectoryIndexOptions>
    {
    
        public void PostConfigure(string name, LuceneDirectoryIndexOptions options)
        {
            switch (name)
            {
                case "ExternalIndex":
                    options.FieldDefinitions.AddOrUpdate(
                        new FieldDefinition("newsDate",//_umbracoModelHelperService.GetPropertyAlias<NewsItemDataPage>(nameof(NewsItemDataPage.NewsDate)),
                        FieldDefinitionTypes.DateTime));
                    break;
            }
        }
    }
    

    and you should register the configuration in startup

            services.ConfigureOptions<ConfigureCustomIndexOptions>();
    
Please Sign in or register to post replies

Write your reply to:

Draft