Copied to clipboard

Flag this post as spam?

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


  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 02:10
    George Phillipson
    0

    Lucene Search Not Working on range

    Hi I have the following code and I cannot get it to work when searching with range.

    Can anyone help with this problem, my code is below.

    var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["AuctionSearchSearcher"];
    
                ISearchCriteria searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.And);
    
                IBooleanOperation query;
                if (paddedLower != string.Empty && paddedHigher != string.Empty)
                {
                    query = searchCriteria.Range("bidPrice", paddedLower, paddedHigher,true,true);//.Or().Field("nodeName", q.Boost(3)).Or().Field("description", q.Fuzzy());
                }
                else
                {
                    query = searchCriteria.Field("nodeName", q.Boost(3)).Or().Field("description", q.Fuzzy());
                }
    

    Thanks in advance

  • Pawel Bres 39 posts 160 karma points c-trib
    Mar 18, 2018 @ 11:09
    Pawel Bres
    0

    hi George, could you extract raw Lucene query you are getting?

    The second question, are you padding bidPrice same way in OnGatheringNodeData to keep the same format?

  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 12:18
    George Phillipson
    0

    Hi Pawel

    Not sure what you mean by return raw query, but I have done this

    ISearchCriteria query1 = searchCriteria.Range("bidPrice", paddedLower, paddedHigher, true, true).Compile().RawQuery(q);

    Result:

    SearchIndexType: "", LuceneQuery: {+bidPrice:[10.00 TO 100.00] +bidPrice:[10.00 TO 100.00] +(id:tack nodeName:tack updateDate:tack writerName:tack nodeTypeAlias:tack description:tack)}

    What is 'OnGatheringNodeData' as I have not done that

    Regards George

  • Pawel Bres 39 posts 160 karma points c-trib
    Mar 18, 2018 @ 20:21
    Pawel Bres
    0

    hi George,

    basically, Lucene cannot compare numeric values at all, it's comparing all values as strings. So to make it work you need to prepare your values before storing them in Examine index. Otherwise, Lucene will see them as strings, so if you, for example, have 1, 11, 50 and 100, the order, using string sort will be: 1 100 11 50

    to make them sort as numbers you need to pad them left and bring them to the same format, e.g. 00001, 00011, 00050, 00100. Then when you sort them as strings you will get:

    00001 00011 00050 00100

    you can do it like this:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {        
    ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData += OnGatheringNodeData;
    base.ApplicationStarted(umbracoApplication, applicationContext);
    }
    
    
    private void OnGatheringNodeData(object sender, IndexingNodeDataEventArgs e)
    {
                if (e.Fields.ContainsKey("bidPrice"))
                {
                    e.Fields["searchBidPrice"] = e.Fields["bidPrice"].PadLeft(10,'0');
                }
    }
    

    and then:

    searchCriteria.Range("searchBidPrice", paddedLower, paddedHigher,true,true)
    
  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 20:57
    George Phillipson
    0

    Hi Pawel

    I must be still doing something wrong, I get the following error:

    Format specifier was invalid. My code is below

     decimal paddedLower      = (decimal)0.00;
                decimal paddedHigher     = (decimal)0.00;
    
    
                if (minPrice != string.Empty && maxPrice != string.Empty)
                {
                    paddedLower = decimal.Parse(minPrice);
                    paddedHigher = decimal.Parse(maxPrice);
                }
    
    
                var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["AuctionSearchSearcher"];
    
                ISearchCriteria searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.And);
    
                IBooleanOperation query;
                if (paddedLower != (decimal) 0.00 && paddedHigher != (decimal)0.00)
                {
                    query  = searchCriteria.Range("bidPrice", paddedLower.ToString("D6"), paddedHigher.ToString("D6"), true, true);//.Or().Field("nodeName", q.Boost(3)).Or().Field("description", q.Fuzzy());
                    //ISearchCriteria query1 = searchCriteria.Range("bidPrice", paddedLower, paddedHigher, true, true).Compile().RawQuery(q);
                }
    
    > <IndexUserFields>
    >       <add Name="description"/>
    >       <add Name="image"/>
    >       <add Name="name"/>
    >       <add Name="bidPrice"/>
    >     </IndexUserFields>
    

    Thanks George

  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 21:08
    George Phillipson
    0

    Hi Pawel

    I have removed the D6, but now it returns all values that are also outside the range

    Also when doing search in backoffice, padding is not added to field

    _Icon: icon-shopping-basket color-yellow _IndexType: content _Key: 24fb8acf-9533-43e0-afa7-ad11ac16a8e7 _NodeId: 1075 _NodeTypeAlias: itemsforsale _Path: -1,1056,1073,1075 bidEndDateTime: 2018-03-20T10:00:00 bidPrice: 30.01 description:

    Regards

    George

  • Pawel Bres 39 posts 160 karma points c-trib
    Mar 18, 2018 @ 22:01
    Pawel Bres
    100

    ok, so you are using IndexUserFields :)

    if you check https://our.umbraco.org/documentation/reference/config/examinesettings/

    you can find this info:

    • Type is the data type of the field. By default the data type is a string Data types supported are:

      NUMBER, INT, FLOAT, DOUBLE, LONG, DATE, DATETIME, DATE.YEAR, DATE.MONTH,
      DATE.DAY, DATE.HOUR, DATE.MINUTE
      

    try to set:

    <add Name="bidPrice" EnableSorting="true" Type="DOUBLE" />

  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 22:06
    George Phillipson
    0

    Hi Pawel

    Just been checking that, have changed to double and about to test.

    Hopefully will work

    Regards George

  • George Phillipson 108 posts 287 karma points
    Mar 18, 2018 @ 22:18
    George Phillipson
    0

    Hi Pawel

    Thanks, that now works :)

    Regards George

Please Sign in or register to post replies

Write your reply to:

Draft