cpoDesign
cpoDesign

Reputation: 9143

Examine search contain words

I am trying to write script using examine fluent api.

I have conditions that I need to fulfill

I am able to match only words starting with this string.

When I execute to code below, I do get only words starting with the searchTerm

   public IEnumerable<SearchResultItem> Search(string searchTerm)
        {
           //Create search Criteria
            var sc = ExamineManager.Instance.CreateSearchCriteria();

            //define query
            var query = sc.NodeName(searchTerm.MultipleCharacterWildcard())
                        .Or()
                        .Field("content", searchTerm.MultipleCharacterWildcard())
                        .Compile();


            var results = ExamineManager.Instance.SearchProviderCollection["ContentSearcher"].Search(query);                

            return results.OrderBy(x => x.Score).Select(MapSearchResults);
        }

How do I update the search script for all conditions?

Upvotes: 1

Views: 3017

Answers (1)

stoian tankovski
stoian tankovski

Reputation: 11

Solution with raw query. This should hover search must find nodes starting with searchTerm search must find nodes containg searchTerm search must find nodes ending with searchTerm search must support multiple words

  var searchTerm = Request["term"].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
          var searchCriteria = searcher.CreateSearchCriteria();
          var luceneString = new System.Text.StringBuilder();
          luceneString.Append("nodeTypeAlias:");

          luceneString.Append("*");

          for (int i = 0; i < searchTerm.Length; i++)
          {

              luceneString.Append(" AND ");

              luceneString.Append("title:");
              luceneString.Append("*");
              luceneString.Append(searchTerm[i]);
              luceneString.Append("*");
          }

          var query = searchCriteria.RawQuery(luceneString.ToString());
          var searchResults = searcher.Search(query);

this article helped me http://www.lucenetutorial.com/lucene-query-syntax.html

Upvotes: 1

Related Questions