private ICollection <ISolrQuery> BuildFilterQueries(SearchParameters parameters)
 {
     return(parameters.Facets
            .Where(f => !string.IsNullOrWhiteSpace(f.Value))
            .Select(f => new SolrQueryByField(f.Name, f.Value))
            .ToList <ISolrQuery>());
 }
        private ISolrQuery BuildQuery(SearchParameters parameters)
        {
            var        text = parameters.Text;
            ISolrQuery query;

            if (!string.IsNullOrEmpty(text))
            {
                text = text.Replace(":", " ");
                text = text.Replace("!", " ");
                text = text.Replace("{", " ");
                text = text.Replace("}", " ");
                text = text.Replace("[", " ");
                text = text.Replace("]", " ");
                text = text.Replace("(", " ");
                text = text.Replace(")", " ");
                text = text.Replace("^", " ");

                var lang = "";
                if (!CultureHelper.IsNorwegian())
                {
                    lang = "_" + Culture.EnglishCode;
                }

                if (text.Trim().Length == 0)
                {
                    query = SolrQuery.All;
                }
                else if (text.Trim().Length < 5)
                {
                    query = new SolrMultipleCriteriaQuery(new[]
                    {
                        new SolrQuery("registerText" + lang + ":" + text + "^50"),
                        new SolrQuery("registerItemText" + lang + ":" + text + "^50"),
                        new SolrQuery("registerText" + lang + ":" + text + "*^40"),
                        new SolrQuery("registerItemText" + lang + ":" + text + "*^40"),
                        new SolrQuery("allText" + lang + ":" + text + "^1.2"),
                        new SolrQuery("allText" + lang + ":" + text + "*^1.1")
                    });
                }
                else
                {
                    query = new SolrMultipleCriteriaQuery(new[]
                    {
                        new SolrQuery("registerText" + lang + ":\"" + text + "\"^50"),
                        new SolrQuery("registerItemText" + lang + ":\"" + text + "\"^50"),
                        new SolrQuery("allText" + lang + ":\"" + text + "\"~1"), //Fuzzy
                        new SolrQuery("allText2" + lang + ":\"" + text + "\"")   //Stemmer
                        //new SolrQuery("allText3:" + text)        //Fonetisk
                    });
                }
            }
            else
            {
                query = SolrQuery.All;
            }

            Log.Debug("Query: " + query.ToString());
            return(query);
        }
 private static FacetParameters BuildFacetParameters(SearchParameters parameters)
 {
     return(new FacetParameters
     {
         Queries = parameters.Facets.Select(item =>
                                            new SolrFacetFieldQuery(item.Name)
         {
             MinCount = 1, Limit = 150, Sort = false
         }
                                            ).ToList <ISolrFacetQuery>()
     });
 }
Exemple #4
0
        public IHttpActionResult SearchByOrganizationName(string name)
        {
            List <Models.Api.Item> resultat   = new List <Models.Api.Item>();
            SearchParameters       parameters = new SearchParameters();

            parameters.Text = name;
            SearchResult searchResult = _searchService.Search(parameters);

            foreach (var it in searchResult.Items)
            {
                resultat.Add(Convert(it));
            }
            return(Ok(resultat));
        }
        public SearchResult Search(SearchParameters parameters)
        {
            ISolrQuery query = BuildQuery(parameters);
            var        order = new[] { new SortOrder("score", Order.DESC) };

            if (parameters.OrderBy == "name")
            {
                order = new[] { new SortOrder("RegisterItemName", Order.ASC) };
            }
            else if (parameters.OrderBy == "date_updated")
            {
                order = new[] { new SortOrder("RegisterItemUpdated", Order.DESC) };
            }
            else if (string.IsNullOrWhiteSpace(parameters.Text) && HasNoFacetvalue(parameters.Facets))
            {
                order = new[] { new SortOrder("RegisterItemName", Order.ASC) };
            }
            else if (parameters.OrderBy == "score")
            {
                order = new[] { new SortOrder("score", Order.DESC) };
            }
            try
            {
                SolrQueryResults <RegisterIndexDoc> queryResults = _solrInstance.Query(query, new QueryOptions
                {
                    FilterQueries = BuildFilterQueries(parameters),
                    OrderBy       = order,
                    Rows          = parameters.Limit,
                    Start         = parameters.Offset - 1,
                    Facet         = BuildFacetParameters(parameters),

                    Fields = new[] { "SystemID", "RegisterName", "RegisterDescription", "RegisterItemName", "RegisterItemName_en", "RegisterItemDescription", "RegisterID", "Discriminator", "RegisterItemUpdated", "type",
                                     "ParentRegisterUrl", "RegisteItemUrl", "SubregisterUrl", "subregisterItemUrl", "theme", "organization" }
                });
                return(CreateSearchResults(queryResults, parameters));
            }
            catch (Exception ex)
            {
                Log.Error("Error in search", ex);

                return(CreateSearchResults(null, parameters));
            }
        }
        private SearchResult CreateSearchResults(SolrQueryResults <RegisterIndexDoc> queryResults, SearchParameters parameters)
        {
            List <SearchResultItem> items = ParseResultDocuments(queryResults);

            List <Facet> facets = ParseFacetResults(queryResults);

            return(new SearchResult
            {
                Items = items,
                Facets = facets,
                Limit = parameters.Limit,
                Offset = parameters.Offset,
                NumFound = queryResults.NumFound
            });
        }