2

Resolved

Lucene does not filter type correctly

description

I have added the following: searchBuilder.WithField("type", "BlogPost").ExactMatch().AsFilter();

Which gets added to Lucene with the following Method:
  public ISearchBuilder WithField(string field, string value) {
        CreatePendingClause();

        if ( !String.IsNullOrWhiteSpace(value) ) {
            _query = new TermQuery(new Term(field, QueryParser.Escape(value.ToLower())));
        }           
        return this;
    }
The following fails because of the:
value.ToLower()

I have removed the ToLower in testing and have found that I can filter by "type" correctly.

This was previously discussed here:
https://orchard.codeplex.com/discussions/248022

comments

jao28 wrote Jul 23, 2014 at 7:55 PM

I had an open ticket here: https://orchard.codeplex.com/workitem/20285 which was closed (though I don't agree with the answer but since it was closed it is likely no one saw my final comments showing how it didn't work out). Hopefully between these two tickets it can be resolved.

manudea wrote Aug 14, 2014 at 9:52 AM

After further thinking and suggetsion on discussion I agree that the best is do not touch lucene and change the way types are indexed on Common part:

Orchard.Web\Core\Common\Handlers\CommonPartHandler.cs(51)

from:
  .Add("type", commonPart.ContentItem.ContentType).Store()
to:
  .Add("type", commonPart.ContentItem.ContentType.ToLowerInvariant()).Store()
Anyway DO NOT CLOSE the ticket witout a FIX because the current code si BROKEN! and does not index types in a way can be searched so it is pretty useless.
I strongly suggest to add some test to ensure that types are indexed correctly.

sebastienros wrote Aug 14, 2014 at 6:14 PM

Sorry guys, I had not seen the .toLower() in the query. This is the actual bug. I try to understand why it's there an fix it.

sebastienros wrote Aug 15, 2014 at 2:22 AM

Fixed in changeset 43a2e1a282d1c96105c8d1befc89fb82c30aee18

sebastienros wrote Aug 15, 2014 at 2:24 AM

I have fixed the issue by adding a new search API option: .NotAmalyzed() which will prevent a search term from being tokenized/lowercased. This should be used anytime you want to search for a technical field, i.e. one which is not analyzed during indexation.

In your example it will work like this:

searchBuilder.WithField("type", "BlogPost").NotAnalyzed().ExactMatch().AsFilter();