private void SearchOptions(string searchText, bool fuzzy, bool wildcard, CustomSearchEventArgs customSearchEventArgs)
        {
            IModelElasticSearchFieldsItem item = null;

            if (FilterFieldsAction.Active && FilterFieldsAction.SelectedItem != null && FilterFieldsAction.SelectedItem.Model != null)
            {
                item = FilterFieldsAction.SelectedItem.Model as IModelElasticSearchFieldsItem;
            }
            customSearchEventArgs.Json = ElasticSearchClient.Instance.SearchBody(searchText, ElasticSearchResults, fuzzy, wildcard, customSearchEventArgs.Filter, customSearchEventArgs.SecurityFilter, item);
            OnCustomSearch(customSearchEventArgs);
            lastSearchText = customSearchEventArgs.SearchText;
        }
        private HitsMetaData DoFuzzySearch(CustomSearchEventArgs customSearchEventArgs, bool wildcard, string searchText)
        {
            HitsMetaData hits = null;

            customSearchEventArgs.Retry = true;
            if (wildcard)
            {
                searchText = searchText.AddStringToWordEnd("~");
            }
            SearchOptions(searchText, true, wildcard, customSearchEventArgs);
            lastSearchElastic = customSearchEventArgs.Handled;
            _ElasticCanFuzzy  = false;
            if (!lastSearchElastic)
            {
                hits = ElasticSearchClient.Instance.Search(customSearchEventArgs.Indexes, customSearchEventArgs.Types, customSearchEventArgs.Json);
            }
            return(hits);
        }
 /// <summary>
 /// Invokes a Custom Search event handler
 /// </summary>
 /// <param name="args">A CustomSearchEventArgs instance</param>
 protected virtual void OnCustomSearch(CustomSearchEventArgs args)
 {
     CustomSearch?.Invoke(this, args);
 }
 protected override void FullTextSearch(ParametrizedActionExecuteEventArgs args)
 {
     if (args == null)
     {
         throw new ArgumentNullException(nameof(args));
     }
     WaitScreen.Instance.Show(CaptionHelper.GetLocalizedText(ElasticSearchClient.MessageGroup, "FullTextSearchWaitScreenCaption"), CaptionHelper.GetLocalizedText(ElasticSearchClient.MessageGroup, "FullTextSearchWaitScreenText"));
     try
     {
         if (RefreshBeforeSearch)
         {
             ObjectSpace.Refresh();
         }
         lastSearchElastic = false;
         if (IsElasticSearchAvailable)
         {
             var ci = BYteWareTypeInfo.GetBYteWareTypeInfo(View.ObjectTypeInfo.Type);
             if (!ci.ElasticIndexError)
             {
                 var  searchText = args.ParameterCurrentValue == null ? string.Empty : args.ParameterCurrentValue.ToString();
                 bool fuzzy;
                 bool wildcard;
                 if (string.IsNullOrEmpty(searchText))
                 {
                     searchText = "*";
                 }
                 searchText = ElasticSearchClient.PrepareSearchText(searchText, out fuzzy, out wildcard);
                 var filter = string.Empty;
                 if (SetFilterAction.Active && SetFilterAction.SelectedItem != null && SetFilterAction.SelectedItem.Model != null)
                 {
                     var filterExtension = SetFilterAction.SelectedItem.Model as IModelListViewFilterItemElasticSearch;
                     if (filterExtension != null)
                     {
                         filter = filterExtension.ElasticSearchFilter ?? string.Empty;
                     }
                 }
                 var customSearchEventArgs = new CustomSearchEventArgs(
                     args.ParameterCurrentValue == null ? string.Empty : args.ParameterCurrentValue.ToString(),
                     ci.ESIndexes.ToArray(),
                     ci.ESTypes.ToArray(),
                     filter,
                     ci.ESSecurityFilter);
                 SearchOptions(searchText, fuzzy, wildcard, customSearchEventArgs);
                 lastSearchElastic = customSearchEventArgs.Handled;
                 if (!lastSearchElastic)
                 {
                     var hits = ElasticSearchClient.Instance.Search(customSearchEventArgs.Indexes, customSearchEventArgs.Types, customSearchEventArgs.Json);
                     if (hits != null)
                     {
                         _ElasticCanFuzzy = (!wildcard && !fuzzy) || (wildcard && !searchText.EveryWordContains("~"));
                         if (hits.Total == 0 && _ElasticCanFuzzy && !customSearchEventArgs.Retry)
                         {
                             hits = DoFuzzySearch(customSearchEventArgs, wildcard, searchText);
                         }
                         if (!lastSearchElastic && hits != null)
                         {
                             View.CollectionSource.CollectionReloaded -= CollectionSource_CollectionReloaded;
                             View.CollectionSource.CollectionChanged  -= CollectionSource_CollectionReloaded;
                             SaveScores(hits);
                             if (View.CollectionSource.List.Count <= 0 && _ElasticCanFuzzy && !customSearchEventArgs.Retry)
                             {
                                 hits = DoFuzzySearch(customSearchEventArgs, wildcard, searchText);
                                 if (hits != null)
                                 {
                                     SaveScores(hits);
                                 }
                             }
                             if (hits != null)
                             {
                                 if (!View.CollectionSource.IsServerMode)
                                 {
                                     CollectionSource_CollectionReloaded(View.CollectionSource, null);
                                     View.CollectionSource.CollectionReloaded += CollectionSource_CollectionReloaded;
                                     View.CollectionSource.CollectionChanged  += CollectionSource_CollectionReloaded;
                                 }
                                 lastSearchElastic       = true;
                                 SetFilterAction.ToolTip = GetToolTip();
                             }
                         }
                     }
                 }
             }
         }
         if (!lastSearchElastic)
         {
             if (FilterFieldsAction.Active)
             {
                 FilterFieldsAction.Active.SetItemValue(NoElasticSearchReason, false);
             }
             scores.Clear();
             base.FullTextSearch(args);
         }
         else
         {
             if (!FilterFieldsAction.Active[NoElasticSearchReason])
             {
                 FilterFieldsAction.Active.SetItemValue(NoElasticSearchReason, true);
             }
         }
     }
     finally
     {
         WaitScreen.Instance.Hide();
     }
 }