1

I'm trying to create an autocomplete control that returns items stored in a bucket however I seem to be having issues with my search query.

I've created a custom index and can see the items within the index however when I try to search the index using the ContentSearchManager I'm not getting the results I expect.

My current code is:

        using (var context = index.CreateSearchContext())
        {
            const string Id = "{2E1D94CD-DF46-4B45-B3F9-66A002C20CCA}";

            Expression<Func<Symptom, bool>> predicate = s => s.TemplateId == new Sitecore.Data.ID(Id) &&
                    s.LaymanDescription.Contains(search);

            var results = context.GetQueryable<Symptom>().Where(
                predicate).ToList();
         }

As an example I have 2 symptom items

  1. Hair Loss
  2. Hair Loss loss on scalp

It seems that if I search "hair loss on" I still get the hair loss item back. I wasn't expecting this. Looking at the search query in the logs it seems that "hair loss on" is split and then searched on individually.

  1. How can I stop the query being split? Am I using correct method for querying?
  2. How can I return results based on a partial search? e.g if I search on "Hair Lo" I want both the "Hair Loss" and "Hair Loss on scalp", if I search on "Hair Loss o" then I want the "Hair Loss on scalp" returned only.

3 Answers 3

2

From what you've written in your question it looks like your field is tokenized - instead of saving the whole string in the index, lucene creates tokens for full text search.

What you would need to do is to add indexType="UNTOKENIZED" to your index configuration:

<fieldMap ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration/fieldMap">
    <fieldNames hint="raw:AddFieldByFieldName">
        <field indexType="UNTOKENIZED" fieldName="Layman Description" type="System.String" storageType="YES" vectorType="NO" boost="1f" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider"/>
        </field>

That xml assumes that you have custom configuration for your index which refs to the fieldMap from the defaultLuceneIndexConfiguration.

After you change the configs, rebuild the indexes and you should be able to see in lucene the values of your field and search should work based on exact comparison.

3

If you are looking for autocomplete implementation, you can refer to search terms functionality in Sitecore Content Search:

Instead of running search query each time:

var results = context.GetQueryable<Symptom>().Where(predicate).ToList();

it is much more quicker and efficient to use search terms api:

var terms = context.GetTermsByFieldName("LaymanDescription", search)
0

Additionally you can download Luke index viewer(http://www.getopt.org/luke/). It will help you run the lucene queries and get the result. So accordingly you can run queries for below search key words. And luke will help you understand whats exactly stored in your indexes so you can have apple to apple comparison.

1. Hair Loss
2. Hair Loss loss on scalp

Please visit below link to get understanding of Luke tool

https://stackoverflow.com/questions/14167110/show-contents-of-lucene-index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.