1

I got some help in a previous question about prioritizing the title field in searches, however when I run the search now, I continue to to get some results where the word is only in the content field and not in the title field. These hits are interspersed so that there are some hits that don't have the word in the title coming before some that do. It doesn't seem to matter how much I boost the value for the title field and I've tried to manually decrease the priority of the content field, but that doesn't seem to work either. It seems that the basequery is somehow retrieving all hits for any field, and then after that the boosting only helps so much. Any other tips? Here is the code I'm using:

public class CustomSearchResultItem : SearchResultItem
    {
        [DataMember]
        [IndexField("title")]
        public string Title
        {
            get; set;
        }

        [DataMember]
        [IndexField("_content")]
        public string BodyContent { get; set; }
    }

   public List<CustomSearchResultItem> GetSiteSearch(string searchQuery)
    {
        string indexName = App.Core.Config.SettingsManager.GetSetting("SearchIndexName");
        var index = Sitecore.ContentSearch.ContentSearchManager.GetIndex(indexName);
        var db = Sitecore.Context.Database;

        Sitecore.Diagnostics.Error.AssertNotNull(index,
            "There is no " + indexName + " index on the current database (" + db.Name + ")");

        using (var context = index.CreateSearchContext())
        {
            var baseQuery = PredicateBuilder.True<CustomSearchResultItem>(); 

            var query = PredicateBuilder.True<CustomSearchResultItem>();

            if (!string.IsNullOrWhiteSpace(searchQuery))
            {

                foreach (var word in searchQuery.Split(' '))
                {
                    query = query.Or(item => item.Title.Contains(word)).Boost(1000);
                    query = query.Or(item => item.BodyContent.Contains(word)).Boost(0);
                }
            }

            baseQuery = baseQuery.And(query);
            var queryRunner = context.GetQueryable<CustomSearchResultItem>().Where(baseQuery);
            var results = queryRunner.ToList(); /

            return results;


        }
    }
4
  • Have you tried changing the Boost level to something absurd to see if that affects the results strongly enough? Nov 29, 2018 at 4:02
  • Yes, I made the Boost 10000000000000000, and it still shows these other items intermixed that don't have the search word in the title... Nov 29, 2018 at 14:22
  • I think you have a type cast problem. Try passing a float and not an Int. For our boosting we run something along the lines of float BoostValue = float.Parse(StringVarDefinedByUsers.ToString()) / 100;
    – Tyler.S
    Nov 29, 2018 at 18:52
  • Same issue when I use a float... Nov 29, 2018 at 20:01

0

Your Answer

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

Browse other questions tagged or ask your own question.