Пример #1
0
        /// <summary>
        /// Gets all the fields for a given search collection
        /// </summary>
        /// <param name="searchCollectionName">The name of the search collection</param>
        public static string[] GetFields(string searchCollectionName)
        {
            List <string> fieldList = new List <string>();

            ESSiteWideSearchCollectionElement searchCollConfig = GetSearchCollectionConfig(searchCollectionName);

            if (searchCollConfig == null)
            {
                throw new ConfigurationErrorsException("ElasticSearch SiteWideSearch collection, " + searchCollectionName + ", cannot be found in configuration.");
            }

            FieldElementCollection fieldConfigs = searchCollConfig.Fields;

            foreach (FieldElement fieldElement in fieldConfigs)
            {
                fieldList.Add(fieldElement.FieldName);
            }

            return(fieldList.ToArray());
        }
        /// <summary>
        /// Gets the search results from this SiteWideSearch provider.
        /// </summary>
        /// <param name="searchCollection">The search collection.</param>
        /// <param name="term">The term.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="offset">The offset.</param>
        /// <returns></returns>
        public override ISiteWideSearchResultCollection GetSearchResults(string searchCollection, string term, int pageSize, int offset)
        {
            ESSiteWideSearchCollectionElement searchCollConfig = ElasticSearchConfig.GetSearchCollectionConfig(searchCollection);
            ClusterElement clusterConfig  = searchCollConfig.ClusterElementDetails;
            var            connectionPool = new SniffingConnectionPool(searchCollConfig.ClusterNodes);
            var            config         = new ConnectionConfiguration(connectionPool)
                                            .UsePrettyResponses()
                                            .MaximumRetries(clusterConfig.MaximumRetries)//try 5 times if the server is down
                                            .ExposeRawResponse()
                                            .ThrowOnElasticsearchServerExceptions();

            var esClient = new ElasticsearchClient(config);

            string[] fieldList = ElasticSearchConfig.GetFields(searchCollection);


            //do search_template
            var _body = new
            {
                template = new
                {
                    id = searchCollConfig.Template
                },
                @params = new
                {
                    my_value  = term,
                    my_size   = pageSize.ToString(),
                    my_from   = offset.ToString(),
                    my_site   = searchCollConfig.Site,
                    my_fields = fieldList
                }
            };

            ElasticsearchResponse <DynamicDictionary> results = null;

            try
            {
                results = esClient.SearchTemplate(searchCollConfig.Index, _body);
            }
            catch (MaxRetryException ex)
            {
                try
                {
                    //log the maximum retry excpetion
                    log.Error("Error using the ESClient Search Template method. Maximum retry exception: ", ex);
                    //sleep for 5 seconds
                    Thread.Sleep(clusterConfig.ConnectionTimeoutDelay);
                    //try to fetch results again
                    results = esClient.SearchTemplate(searchCollConfig.Index, _body);
                }
                catch (Exception e)
                {
                    log.Error("Error using the ESClient Search Template method.", e);
                    throw e;
                }
            }//retry catch log error sleep retry

            List <ESSiteWideSearchResult> foundTerms = new List <ESSiteWideSearchResult>(pageSize);

            if (results.Success)
            {
                var stringResponse = results.Response;
                try
                {
                    foreach (var hit in results.Response["hits"].hits)
                    {
                        //title is a special case and when it is empty the value needs to be Untitled
                        string title = SetFieldValue(hit, "title");
                        if (string.IsNullOrEmpty(title))
                        {
                            title = "Untitled";
                        }

                        string url = SetFieldValue(hit, "url");

                        string description = SetFieldValue(hit, "metatag.description");


                        string type = SetFieldValue(hit, "metatag.dcterms.type");


                        foundTerms.Add(new ESSiteWideSearchResult(title, url, description, type));
                    }
                }

                catch (Exception ex)
                {
                    log.Error("Error retrieving search results.", ex);
                    throw ex;
                }
            }

            else
            {
                string prettyResponse = "";
                //log the raw response message
                //if there is not response them log with message - "No message in response"
                if (!string.IsNullOrEmpty(System.Text.Encoding.Default.GetString(results.ResponseRaw)))
                {
                    prettyResponse = System.Text.Encoding.Default.GetString(results.ResponseRaw);
                }
                else
                {
                    prettyResponse = "No message in response";
                }

                log.ErrorFormat("Search failed. Http Status Code: {0}. Message: {1}", results.HttpStatusCode.Value, prettyResponse);

                throw (new Exception("Search failed. Http Status Code:" + results.HttpStatusCode.Value.ToString() + ". Message: " + prettyResponse));
            }

            //Return the results
            return(new ESSiteWideSearchResultCollection(foundTerms.AsEnumerable())
            {
                ResultCount = results.Response["hits"].total
            });
        }