/// <summary> /// Builds the Search index. /// </summary> protected virtual ISearchIndex BuildSearchIndex(EdismaxSolrQuery query) { if (query != null && !string.IsNullOrWhiteSpace(query.IndexName)) { return(ContentSearchManager.GetIndex(query.IndexName)); } // Build the index using the current context. Item homeItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath); var indexable = new SitecoreIndexableItem(homeItem); return(ContentSearchManager.GetIndex(indexable)); }
/// <summary> /// Search for content using an instance of <see cref="EdismaxSolrQuery"/>. /// </summary> protected virtual SolrQueryResults <T> EdismaxSearch <T>( string q, Item solrQueryItem, int startRow = 0, string language = "", ICollection <ISolrQuery> additionalFilters = null) { // Validate the parameters. if (string.IsNullOrWhiteSpace(q) || solrQueryItem == null) { return(new SolrQueryResults <T>()); } // Build the EdismaxSolrQuery object from the Sitecore item. EdismaxSolrQuery query = BuildEdismaxSolrQuery(solrQueryItem); return(EdismaxSearch <T>(q, query, startRow, language, additionalFilters)); }
/// <summary> /// Search for content using a Sitecore <see cref="Item"/>. /// </summary> protected virtual SolrQueryResults <T> EdismaxSearch <T>( string q, EdismaxSolrQuery query, int startRow = 0, string language = "", ICollection <ISolrQuery> additionalFilters = null) { // Validate the parameters. if (string.IsNullOrWhiteSpace(q) || query == null) { return(new SolrQueryResults <T>()); } // Filters: Base Filters. var filters = new List <ISolrQuery>(); // Base Filters: Language language = !string.IsNullOrWhiteSpace(language) ? language : Sitecore.Context.Language.Name; filters.Add(new SolrQueryByField("_language", language)); // Base Filters: do NOT return standard values items. filters.Add(!new SolrQueryByField("_name", Sitecore.Constants.StandardValuesItemName)); // Filters: Locations. SolrMultipleCriteriaQuery locationsQuery = BuildMultipleCriteriaQuery(query.Locations, "_path"); if (locationsQuery != null) { filters.Add(locationsQuery); } // Filters: Templates. SolrMultipleCriteriaQuery templateQuery = BuildMultipleCriteriaQuery(query.Templates, "_template"); if (templateQuery != null) { filters.Add(templateQuery); } // Additional Filters if (additionalFilters != null && additionalFilters.Any()) { filters.AddRange(additionalFilters); } // Edismax Parameters var extraParams = new List <KeyValuePair <string, string> >(); // Use Edismax extraParams.Add(new KeyValuePair <string, string>("defType", "edismax")); // Query Fields (qf) string qf = BuildFlatBoostString(query.QueryFields); if (!string.IsNullOrWhiteSpace(qf)) { extraParams.Add(new KeyValuePair <string, string>("qf", qf)); } // Query Tie if (query.Tie > 0) { extraParams.Add(new KeyValuePair <string, string>("tie", query.Tie.ToString())); } // Minimum Match if (!string.IsNullOrWhiteSpace(query.MinimumMatch)) { extraParams.Add(new KeyValuePair <string, string>("mm", query.MinimumMatch)); } // Minimum Match Auto Relax if (!string.IsNullOrWhiteSpace(query.MinimumMatchAutoRelax)) { extraParams.Add(new KeyValuePair <string, string>("mm.autoRelax", query.MinimumMatchAutoRelax)); } // Phrase Fields (pf) string pf = BuildFlatBoostString(query.PhraseFields); if (!string.IsNullOrWhiteSpace(pf)) { extraParams.Add(new KeyValuePair <string, string>("pf", pf)); } // Phrase Fields 2 (pf2) string pf2 = BuildFlatBoostString(query.PhraseFields2); if (!string.IsNullOrWhiteSpace(pf2)) { extraParams.Add(new KeyValuePair <string, string>("pf2", pf2)); } // Phrase Fields 3 (pf3) string pf3 = BuildFlatBoostString(query.PhraseFields3); if (!string.IsNullOrWhiteSpace(pf3)) { extraParams.Add(new KeyValuePair <string, string>("pf3", pf3)); } // PhraseSlop (ps) if (query.PhraseSlop > 0) { extraParams.Add(new KeyValuePair <string, string>("ps", query.PhraseSlop.ToString())); } // Phrase Slop 2 (ps2) if (query.PhraseSlop2 > 0) { extraParams.Add(new KeyValuePair <string, string>("ps2", query.PhraseSlop2.ToString())); } // Phrase Slop 3 (ps3) if (query.PhraseSlop3 > 0) { extraParams.Add(new KeyValuePair <string, string>("ps3", query.PhraseSlop3.ToString())); } // Boost Query (bq) if (!string.IsNullOrWhiteSpace(query.BoostQuery)) { extraParams.Add(new KeyValuePair <string, string>("bq", query.BoostQuery)); } // Boost Function (bf) if (!string.IsNullOrWhiteSpace(query.BoostFunction)) { extraParams.Add(new KeyValuePair <string, string>("bf", query.BoostFunction)); } // Boost (boost) if (!string.IsNullOrWhiteSpace(query.Boost)) { extraParams.Add(new KeyValuePair <string, string>("boost", query.Boost)); } // Facets if (query.Facet) { extraParams.Add(new KeyValuePair <string, string>("facet", "true")); // Facet Field if (!string.IsNullOrWhiteSpace(query.FacetField)) { extraParams.Add(new KeyValuePair <string, string>("facet.field", query.FacetField)); } // Facet Query (facet.query) if (!string.IsNullOrWhiteSpace(query.FacetQuery)) { extraParams.Add(new KeyValuePair <string, string>("facet.query", query.FacetQuery)); } // Facet Prefix (facet.prefix) if (!string.IsNullOrWhiteSpace(query.FacetPrefix)) { extraParams.Add(new KeyValuePair <string, string>("facet.prefix", query.FacetPrefix)); } } // Spellcheck Dictionary (spellcheck.dictionary): if (query.Spellcheck) { extraParams.Add(new KeyValuePair <string, string>("spellcheck", "true")); // Spellcheck Dictionary Name, as set in solr configs. (spellcheck.dictionary) if (!string.IsNullOrWhiteSpace(query.SpellcheckDictionary)) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.dictionary", query.SpellcheckDictionary)); } // Spellcheck Dictionary (spellcheck.count): if (query.SpellcheckCount > 0) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.count", query.SpellcheckCount.ToString())); } extraParams.Add(new KeyValuePair <string, string>("spellcheck.onlyMorePopular", query.SpellcheckOnlyMorePopular.ToString().ToLowerInvariant())); extraParams.Add(new KeyValuePair <string, string>("spellcheck.extendedResults", query.SpellcheckExtendedResults.ToString().ToLowerInvariant())); // Spellcheck collate if (query.SpellcheckCollate) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.collate", "true")); // Spellcheck Max Collations (spellcheck.maxCollations): if (query.SpellcheckCount > 0) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.maxCollations", query.SpellcheckMaxCollations.ToString())); } // Spellcheck Max Collations (spellcheck.maxCollationTries): if (query.SpellcheckMaxCollationTries > 0) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.maxCollationTries", query.SpellcheckMaxCollationTries.ToString())); } // Spellcheck Max Collations (spellcheck.accuracy): if (query.SpellcheckAccuracy > 0) { extraParams.Add(new KeyValuePair <string, string>("spellcheck.accuracy", query.SpellcheckAccuracy.ToString())); } // Spellcheck Collate Extended Results (spellcheck.collateExtendedResults) extraParams.Add(new KeyValuePair <string, string>("spellcheck.collateExtendedResults", query.SpellcheckCollateExtendedResults.ToString().ToLowerInvariant())); } } // Highlight if (query.Highlight) { extraParams.Add(new KeyValuePair <string, string>("hl", "true")); // Highlight Fields (hl.fl) if (!string.IsNullOrWhiteSpace(query.HighlightFields)) { extraParams.Add(new KeyValuePair <string, string>("hl.fl", query.HighlightFields)); } // Highlight Pre if (!string.IsNullOrWhiteSpace(query.HighlightPre)) { extraParams.Add(new KeyValuePair <string, string>("hl.simple.pre", query.HighlightPre)); } // Highlight Post if (!string.IsNullOrWhiteSpace(query.HighlightPost)) { extraParams.Add(new KeyValuePair <string, string>("hl.simple.post", query.HighlightPost)); } // Hightlight requireFieldMatch extraParams.Add(new KeyValuePair <string, string>("hl.requireFieldMatch", query.HighlightRequireFieldMatch.ToString().ToLowerInvariant())); // Hightlight use Phrase Highlighter extraParams.Add(new KeyValuePair <string, string>("hl.usePhraseHighlighter", query.HighlightUsePhaseHighlighter.ToString().ToLowerInvariant())); // Highlight FragSize extraParams.Add(new KeyValuePair <string, string>("hl.fragsize", query.HighlightFragSize.ToString())); // Highlight Highlight Snippets extraParams.Add(new KeyValuePair <string, string>("hl.snippets", query.HighlightSnippets.ToString())); } // Execute the Solr query. using (IProviderSearchContext ctx = BuildSearchIndex(query).CreateSearchContext()) { SolrQueryResults <T> results = ctx.Query <T>(q, new SolrNet.Commands.Parameters.QueryOptions { Rows = query.Rows, StartOrCursor = new StartOrCursor.Start(startRow), ExtraParams = extraParams, FilterQueries = filters, Debug = query.DebugQuery, OrderBy = BuildOrderBy(query.Sort).ToList() }); return(results); } }