Ejemplo n.º 1
0
        public async Task GetSemanticSearchSuggestions_WhenIndexNotPopulated_ReturnsEmptyArtifactDetails()
        {
            var searchParameters = new SearchEngineParameters(1, 1, true, new HashSet <int>());

            _semanticSearchRepository.Setup(s => s.GetSemanticSearchIndex()).ReturnsAsync((string)null);

            _elasticSearchEngine = new ElasticSearchEngine(_elasticClient.Object, _semanticSearchRepository.Object);

            var result = await _elasticSearchEngine.GetSemanticSearchSuggestions(searchParameters);

            Assert.IsTrue(result.IsEmpty());
        }
Ejemplo n.º 2
0
        public async Task GetSemanticSearchSuggestions_WhenQuerying_ReturnsArtifactDetails()
        {
            var searchParameters = new SearchEngineParameters(1, 1, true, new HashSet <int>());

            _semanticSearchRepository.Setup(s => s.GetSemanticSearchText(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(new SemanticSearchText()
            {
                Name       = "test",
                SearchText = "searchTest"
            });
            SetupIndexAndTypeExists(true, true);
            _semanticSearchRepository.Setup(s => s.GetSemanticSearchIndex()).ReturnsAsync("test");

            var searchItemHit = new Mock <IHit <SemanticSearchItem> >();

            searchItemHit.SetupGet(s => s.Id).Returns("2");
            var searchResponse = new Mock <ISearchResponse <SemanticSearchItem> >();

            searchResponse.SetupGet(s => s.Hits).Returns(new List <IHit <SemanticSearchItem> >()
            {
                searchItemHit.Object
            });

            var connectionSettings = new Mock <IConnectionSettingsValues>();

            connectionSettings.SetupGet(c => c.DefaultIndices).Returns(new FluentDictionary <Type, string>());
            _elasticClient.Setup(
                e => e.SearchAsync <SemanticSearchItem>(It.IsAny <ISearchRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(searchResponse.Object);

            _elasticClient.SetupGet(e => e.ConnectionSettings).Returns(connectionSettings.Object);

            _semanticSearchRepository.Setup(s => s.GetSuggestedArtifactDetails(It.IsAny <List <int> >(), It.IsAny <int>()))
            .ReturnsAsync(new List <ArtifactSearchResult>()
            {
                new ArtifactSearchResult()
                {
                    ItemId = 2
                }
            });

            _elasticSearchEngine = new ElasticSearchEngine(_elasticClient.Object, _semanticSearchRepository.Object);

            var result = await _elasticSearchEngine.GetSemanticSearchSuggestions(searchParameters);

            Assert.IsTrue(result.Count() == 1);
            Assert.IsTrue(result.FirstOrDefault().Id == 2);
        }
Ejemplo n.º 3
0
 public abstract Task <IEnumerable <ArtifactSearchResult> > GetSemanticSearchSuggestions(SearchEngineParameters searchEngineParameters);
Ejemplo n.º 4
0
        public override async Task <IEnumerable <ArtifactSearchResult> > GetSemanticSearchSuggestions(SearchEngineParameters searchEngineParameters)
        {
            var itemIds = await SemanticSearchRepository.GetItemSimilarItemIds(
                searchEngineParameters.ArtifactId,
                searchEngineParameters.UserId,
                searchEngineParameters.PageSize,
                searchEngineParameters.IsInstanceAdmin,
                searchEngineParameters.AccessibleProjectIds);

            return(await GetArtifactSearchResultsFromItemIds(itemIds, searchEngineParameters.UserId));
        }
Ejemplo n.º 5
0
 public async Task <IEnumerable <ArtifactSearchResult> > GetSemanticSearchSuggestions(SearchEngineParameters searchEngineParameters)
 {
     return(await _searchEngine.GetSemanticSearchSuggestions(searchEngineParameters));
 }
Ejemplo n.º 6
0
        public override async Task <IEnumerable <ArtifactSearchResult> > GetSemanticSearchSuggestions(SearchEngineParameters searchEngineParameters)
        {
            try
            {
                var index = await SemanticSearchRepository.GetSemanticSearchIndex();

                if (String.IsNullOrEmpty(index))
                {
                    // Returning empty results when index has not been created yet.
                    return(new List <ArtifactSearchResult>());
                }
                PerformIndexHealthCheck(index);

                // Setting default index name on the connection, otherwise the search request will fail
                _elasticClient.ConnectionSettings.DefaultIndices.Clear();
                _elasticClient.ConnectionSettings.DefaultIndices.Add(typeof(SemanticSearchItem), index);

                var searchText = await SemanticSearchRepository.GetSemanticSearchText(searchEngineParameters.ArtifactId,
                                                                                      searchEngineParameters.UserId);

                // Create the bool query descripter that just searchs for the searchText we constructed
                var boolQueryDescriptor = new BoolQueryDescriptor <SemanticSearchItem>();
                boolQueryDescriptor.Must(GetMoreLikeThisQuery(searchText));

                // Dont return back result for the current artifact id we're searching against
                boolQueryDescriptor.MustNot(GetArtifactIdMatchQuery(searchEngineParameters.ArtifactId));

                // If not instance admin, use the list of accessible project ids to filter out, otherwise no need to filter
                if (!searchEngineParameters.IsInstanceAdmin)
                {
                    boolQueryDescriptor.Filter(GetContainsProjectIdsQuery(searchEngineParameters.AccessibleProjectIds));
                }

                // Creates the search descriptor
                var searchDescriptor = new SearchDescriptor <SemanticSearchItem>();
                searchDescriptor.Index(index).Size(searchEngineParameters.PageSize).Query(q => q.Bool(b => boolQueryDescriptor));
                var results = await _elasticClient.SearchAsync <SemanticSearchItem>(searchDescriptor);

                var hits    = results.Hits;
                var itemIds = new List <int>();
                hits.ForEach(a =>
                {
                    int output;
                    if (Int32.TryParse(a.Id, out output))
                    {
                        itemIds.Add(output);
                    }
                });

                // parse the artifact ids into a artifactsearchresult to return to the caller
                return(await GetArtifactSearchResultsFromItemIds(itemIds.Distinct().ToList(), searchEngineParameters.UserId));
            }
            catch (Exception ex)
            {
                if (ex is ElasticsearchConfigurationException)
                {
                    throw;
                }
                throw new ElasticsearchException(I18NHelper.FormatInvariant("Elastic search failed to process search. Exception:{0}", ex));
            }
        }