/// <summary>
        /// Build and initialize an IdmNet Client object ready for use
        /// </summary>
        /// <returns>Newly initialized IdmNet Client</returns>
        public static IdmNetClient BuildClient()
        {
            var soapBinding = new IdmSoapBinding();
            string fqdn = GetEnvironmentSetting("MIM_fqdn");
            var endpointIdentity = EndpointIdentity.CreateSpnIdentity("FIMSERVICE/" + fqdn);

            var enumerationPath = "http://" + fqdn + SoapConstants.EnumeratePortAndPath;
            var factoryPath = "http://" + fqdn + SoapConstants.FactoryPortAndPath;
            var resourcePath = "http://" + fqdn + SoapConstants.ResourcePortAndPath;

            var enumerationEndpoint = new EndpointAddress(new Uri(enumerationPath), endpointIdentity);
            var factoryEndpoint = new EndpointAddress(new Uri(factoryPath), endpointIdentity);
            var resourceEndpoint = new EndpointAddress(new Uri(resourcePath), endpointIdentity);

            var searchClient = new SearchClient(soapBinding, enumerationEndpoint);
            var factoryClient = new ResourceFactoryClient(soapBinding, factoryEndpoint);
            var resourceClient = new ResourceClient(soapBinding, resourceEndpoint);

            var credentials = new NetworkCredential(
                GetEnvironmentSetting("MIM_username"),
                GetEnvironmentSetting("MIM_pwd"),
                GetEnvironmentSetting("MIM_domain"));

            searchClient.ClientCredentials.Windows.ClientCredential = credentials;
            factoryClient.ClientCredentials.Windows.ClientCredential = credentials;
            resourceClient.ClientCredentials.Windows.ClientCredential = credentials;

            var it = new IdmNetClient(searchClient, factoryClient, resourceClient);
            return it;
        }
示例#2
0
 public void RequestsTheCorrectUrl()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     client.SearchRepo(new SearchRepositoriesRequest("something"));
     connection.Received().GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
 }
示例#3
0
 public void RequestsTheCorrectUrl()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     client.SearchUsers(new SearchUsersRequest("something"));
     connection.Received().Get<SearchUsersResult>(Arg.Is<Uri>(u => u.ToString() == "search/users"), Arg.Any<Dictionary<string, string>>());
 }
示例#4
0
 public void TestingTheTermParameter()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     var request = new SearchUsersRequest("github");
     client.SearchUsers(request);
     connection.Received().Get<SearchUsersResult>(
         Arg.Is<Uri>(u => u.ToString() == "search/users"),
         Arg.Is<Dictionary<string, string>>(d => d["q"] == "github"));
 }
            public override ElasticResponse RefreshFromConnection(SearchClient cli)
            {
                var rawAliases = cli.GetAliases();
                if (rawAliases.HasData)
                {
                    var result = rawAliases.Data.Where(a => a.Value?.Aliases != null && a.Value.Aliases.Count > 0).ToDictionary(a => a.Key, a => a.Value.Aliases.Keys.ToList());
                    Aliases = result;
                }

                return rawAliases;
            }
示例#6
0
 public void TestingTheAccountTypeQualifier_Org()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     var request = new SearchUsersRequest("github");
     request.AccountType = AccountType.Org;
     client.SearchUsers(request);
     connection.Received().Get<SearchUsersResult>(
         Arg.Is<Uri>(u => u.ToString() == "search/users"),
         Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+type:Org"));
 }
示例#7
0
            public void TestingTheStarsQualifier()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                //get repos whos stargazers are greater than 500
                var request = new SearchRepositoriesRequest("github");
                request.Stars = Range.GreaterThan(500);

                client.SearchRepo(request);

                connection.Received().GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
            }
示例#8
0
 public void TestingTheInQualifier()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     //get users where the fullname contains 'github'
     var request = new SearchUsersRequest("github");
     request.In = new[] { UserInQualifier.Fullname };
     client.SearchUsers(request);
     connection.Received().Get<SearchUsersResult>(
         Arg.Is<Uri>(u => u.ToString() == "search/users"),
         Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Fullname"));
 }
            public override ElasticResponse RefreshFromConnection(SearchClient cli)
            {
                var rawHealth = cli.GetClusterHealth();
                if (rawHealth.HasData)
                {
                    var health = rawHealth.Data;
                    Name = health.ClusterName;
                    TotalNodeCount = health.NumberOfNodes;
                    DataNodeCount = health.NumberOfDataNodes;
                    ActiveShards = health.ActiveShards;
                    ActivePrimaryShards = health.ActivePrimaryShards;
                    InitializingShards = health.InitializingShards;
                    RelocatingShards = health.RelocatingShards;
                    UnassignedShards = health.UnassignedShards;
                    StringStatus = health.Status;

                    Indices = health.Indices.Select(i => new NodeIndexInfo
                    {
                        Name = i.Key,
                        StringStatus = i.Value.Status,
                        NumberOfShards = i.Value.NumberOfShards,
                        NumberOfReplicas = i.Value.NumberOfReplicas,
                        ActiveShards = i.Value.ActiveShards,
                        ActivePrimaryShards = i.Value.ActivePrimaryShards,
                        InitializingShards = i.Value.InitializingShards,
                        RelocatingShards = i.Value.RelocatingShards,
                        UnassignedShards = i.Value.UnassignedShards,
                        Shards = i.Value.Shards.Select(s => new NodeIndexShardInfo
                        {
                            Name = s.Key,
                            StringStatus = s.Value.Status,
                            PrimaryActive = s.Value.PrimaryActive,
                            ActiveShards = s.Value.ActiveShards,
                            InitializingShards = s.Value.InitializingShards,
                            RelocatingShards = s.Value.RelocatingShards,
                            UnassignedShards = s.Value.UnassignedShards
                        }).ToList()
                    }).OrderBy(i =>
                    {
                        int j;
                        return int.TryParse(i.Name, out j) ? j : 0;
                    }).ToList();
                }
                else
                {
                    Indices = new List<NodeIndexInfo>();
                }
                return rawHealth;
            }
示例#10
0
            public override ElasticResponse RefreshFromConnection(SearchClient cli)
            {
                var infos = cli.GetClusterNodeInfo().Data;
                Name = infos.ClusterName;
                if (infos.Nodes != null)
                {
                    Nodes = infos.Nodes.Select(node => new NodeInfoWrap
                        {
                            GUID = node.Key,
                            Name = node.Value.Name,
                            Hostname = node.Value.Hostname,
                            VersionString = node.Value.Version,
                            BuildString = node.Value.Build,
                            Attributes = node.Value.Attributes,
                            Info = node.Value,
                        }).OrderBy(node => node.Name).ToList();
                }

                var rawStats = cli.GetClusterNodeStats();
                var stats = rawStats.Data;
                if (stats != null)
                {
                    Name = stats.ClusterName;
                    if (stats.Nodes != null)
                    {
                        foreach (var ns in stats.Nodes)
                        {
                            var ni = Nodes.FirstOrDefault(n => n.GUID == ns.Key);
                            if (ni != null)
                            {
                                ni.Stats = ns.Value;
                            }
                            else
                            {
                                Nodes.Add(new NodeInfoWrap
                                {
                                    GUID = ns.Key,
                                    Name = ns.Value.Name,
                                    Hostname = ns.Value.Hostname,
                                    Stats = ns.Value
                                });
                            }
                        }
                        Nodes = Nodes.OrderBy(n => n.Name).ToList();
                    }
                }
                return rawStats;
            }
示例#11
0
            public override ElasticResponse RefreshFromConnection(SearchClient cli)
            {
                var rawState = cli.GetClusterState();
                if (rawState.HasData)
                {
                    var state = rawState.Data;
                    ClusterName = state.ClusterName;
                    MasterNode = state.MasterNode;
                    Nodes = state.Nodes;
                    RoutingNodes = state.RoutingNodes;
                    if (state.RoutingTable != null)
                        RoutingIndices = state.RoutingTable.Indices;
                }

                return rawState;
            }
示例#12
0
            public override ElasticResponse RefreshFromConnection(SearchClient cli)
            {
                var rawAliases = cli.GetAliases();
                if (rawAliases.HasData)
                {
                    var result = new Dictionary<string, List<string>>();
                    foreach (var a in rawAliases.Data)
                    {
                        if (a.Value != null && a.Value.Aliases != null && a.Value.Aliases.Count > 0)
                            result.Add(a.Key, a.Value.Aliases.Keys.ToList());
                    }
                    Aliases = result;
                }

                return rawAliases;
            }
示例#13
0
        public void PopulateFromConnections(SearchClient client)
        {
            var response = RefreshFromConnection(client);

            // Some implementations are raw
            if (response?.Exception == null) return;
            string lastErrorMessage = response.Exception.Message;
            Exception lastException = response.Exception;

            // Failed to poll all nodes
            if (lastErrorMessage.HasValue())
            {
                throw new Exception($"Failed to poll all elastic nodes for {GetType().Name}: {lastErrorMessage}", lastException);
            }
            throw new Exception($"Failed to poll all elastic nodes for {GetType().Name}");
        }
示例#14
0
 public override ElasticResponse RefreshFromConnection(SearchClient cli)
 {
     var health = cli.GetIndexStats();
     if (health.HasData)
     {
         GlobalStats = health.Data.All;
         Shards = health.Data.Shards;
         Indices = health.Data.Indices;
     }
     else
     {
         GlobalStats = new IndexStats();
         Shards = new ShardCountStats();
         Indices = new Dictionary<string, IndexStats>();
     }
     return health;
 }
        public async Task Query_WhenInvalidIndexName_ReturnsTotalCountOfZero()
        {
            var context = ContextFactory.GetCouchbaseContext();

            context.SearchUris.Add(new FailureCountingUri("http://10.141.151.101:8091/"));
            var fakeMessageHandler = new FakeMessageHandler
            {
                StatusCode = HttpStatusCode.Forbidden,
                Content    = new StringContent("rest_auth: preparePerm, err: index not found ")
            };

            var client   = new SearchClient(new HttpClient(fakeMessageHandler), new SearchDataMapper(), context);
            var response = await client.QueryAsync(new SearchQuery
            {
                Index = "indexdoesnotexist",
                Query = new MatchQuery("foo")
            });

            Assert.AreEqual(0, response.Metrics.TotalCount);
        }
        public async Task Structs()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SimpleStructHotel document = new SimpleStructHotel
            {
                HotelId   = "4",
                HotelName = "Value Inn"
            };

            await resources.GetSearchClient().IndexDocumentsAsync(
                IndexDocumentsBatch.Upload(new[] { document }));

            await resources.WaitForIndexingAsync();

            SearchClient client = resources.GetQueryClient();
            Response <SimpleStructHotel> response = await client.GetDocumentAsync <SimpleStructHotel>(document.HotelId);

            Assert.AreEqual(document, response.Value);
        }
示例#17
0
        }//RunC3FilteredSearch()

        public static void GetC3ProductDetails(ICswResources CswResources, CswNbtC3SearchReturn Return, CswC3SearchParams CswC3SearchParams)
        {
            CswNbtResources _CswNbtResources = (CswNbtResources)CswResources;

            CswNbtC3ClientManager CswNbtC3ClientManager = new CswNbtC3ClientManager(_CswNbtResources, CswC3SearchParams);
            SearchClient          C3SearchClient        = CswNbtC3ClientManager.initializeC3Client();

            if (null != C3SearchClient)
            {
                // For now, the only option here is C3
                CswC3SearchParams.DataService = CswNbtC3ClientManager.DataService;

                CswRetObjSearchResults SearchResults = C3SearchClient.getProductDetails(CswC3SearchParams);
                if (SearchResults.CswC3SearchResults.Length > 0)
                {
                    CswC3Product C3ProductDetails = SearchResults.CswC3SearchResults[0];
                    Return.Data.ProductDetails = C3ProductDetails;
                }
            }
        }
示例#18
0
        public ExternalSearchService()
        {
            // used for testing
            if (_healthIndicatorStore == null)
            {
                _healthIndicatorStore = new BaseUrlHealthIndicatorStore(new NullHealthIndicatorLogger());
            }

            if (_client == null)
            {
                _client = new SearchClient(
                    ServiceUri,
                    "SearchGalleryQueryService/3.0.0-rc",
                    null,
                    _healthIndicatorStore,
                    QuietLog.LogHandledException,
                    new TracingHttpHandler(Trace),
                    new CorrelatingHttpClientHandler());
            }
        }
示例#19
0
        /// <summary>
        /// Creates a new instance of the SearchIndexingBufferedSender.
        /// </summary>
        /// <param name="searchClient">
        /// The SearchClient used to send requests to the service.
        /// </param>
        /// <param name="options">
        /// Provides the configuration options for the sender.
        /// </param>
        internal SearchIndexingBufferedSender(
            SearchClient searchClient,
            SearchIndexingBufferedSenderOptions <T> options = null)
        {
            Argument.AssertNotNull(searchClient, nameof(searchClient));
            SearchClient = searchClient;

            options ??= new SearchIndexingBufferedSenderOptions <T>();
            KeyFieldAccessor = options.KeyFieldAccessor;
            _publisher       = new SearchIndexingPublisher <T>(
                this,
                options.AutoFlush,
                options.AutoFlushInterval,
                options.InitialBatchActionCount,
                options.BatchPayloadSize,
                options.MaxRetries,
                options.RetryDelay,
                options.MaxRetryDelay,
                options.FlushCancellationToken);
        }
        public async Task Query_WhenInvalidUri_ReturnsSuccessCountOfZero()
        {
            var context = ContextFactory.GetCouchbaseContext();

            context.SearchUris.Add(new FailureCountingUri("http://10.141.151.101:8091/"));
            var fakeMessageHandler = new FakeMessageHandler
            {
                StatusCode = HttpStatusCode.NotFound,
                Content    = new StringContent("Requested resource not found. ")
            };

            var client   = new SearchClient(new HttpClient(fakeMessageHandler), new SearchDataMapper(), context);
            var response = await client.QueryAsync(new SearchQuery
            {
                Index = "indexdoesnotexist",
                Query = new MatchQuery("foo")
            });

            Assert.AreEqual(0, response.Metrics.SuccessCount);
        }
示例#21
0
        public async Task Notifications_Added()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync <SimpleDocument>(this);

            SearchClient client = resources.GetSearchClient();

            SimpleDocument[] data = SimpleDocument.GetDocuments((int)(BatchSize * 1.5));

            await using SearchIndexingBufferedSender <SimpleDocument> indexer =
                            client.CreateIndexingBufferedSender(
                                new SearchIndexingBufferedSenderOptions <SimpleDocument>());
            int adds = 0;

            indexer.ActionAddedAsync += (a, c) => { adds++; return(Task.CompletedTask); };
            await indexer.UploadDocumentsAsync(data);

            await DelayAsync(EventDelay, EventDelay);

            Assert.AreEqual(data.Length, adds);
        }
        public void Constructor()
        {
            var serviceName = "my-svc-name";
            var indexName   = "my-index-name";
            var endpoint    = new Uri($"https://{serviceName}.search.windows.net");
            var client      = new SearchClient(endpoint, indexName, new AzureKeyCredential("fake"));

            Assert.NotNull(client);
            Assert.AreEqual(endpoint, client.Endpoint);
            Assert.AreEqual(serviceName, client.ServiceName);
            Assert.AreEqual(indexName, client.IndexName);

            Assert.Throws <ArgumentNullException>(() => new SearchClient(null, indexName, new AzureKeyCredential("fake")));
            Assert.Throws <ArgumentNullException>(() => new SearchClient(endpoint, null, new AzureKeyCredential("fake")));
            Assert.Throws <ArgumentException>(() => new SearchClient(endpoint, string.Empty, new AzureKeyCredential("fake")));
            Assert.Throws <ArgumentNullException>(() => new SearchClient(endpoint, indexName, credential: null));
            Assert.Throws <ArgumentException>(() => new SearchClient(new Uri("http://bing.com"), indexName, credential: null));

            Assert.Throws <ArgumentNullException>(() => new SearchClient(endpoint, indexName, tokenCredential: null));
        }
示例#23
0
        public async Task Dispose_Flushes()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync <SimpleDocument>(this);

            SearchClient client = resources.GetSearchClient();

            SimpleDocument[] data = SimpleDocument.GetDocuments((int)(BatchSize * 1.5));

            await using SearchIndexingBufferedSender <SimpleDocument> indexer =
                            client.CreateIndexingBufferedSender(
                                new SearchIndexingBufferedSenderOptions <SimpleDocument>()
            {
                AutoFlush = false
            });
            AssertNoFailures(indexer);
            await indexer.UploadDocumentsAsync(data);

            await((IAsyncDisposable)indexer).DisposeAsync();
            await WaitForDocumentCountAsync(resources.GetSearchClient(), data.Length);
        }
        public async Task QueryAsync_Sets_LastActivity()
        {
            ConfigContextBase.SearchUris.Add(new FailureCountingUri("http://10.141.151.101:8091/"));
            var handler = FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{ }")
            });

            var client = new SearchClient(new HttpClient(handler), new SearchDataMapper(), new ClientConfiguration());

            Assert.IsNull(client.LastActivity);

            await client.QueryAsync(new SearchQuery
            {
                Index = "index",
                Query = new MatchQuery("foo")
            });

            Assert.IsNotNull(client.LastActivity);
        }
示例#25
0
        public async Task KeyFieldAccessor_Error()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync <SimpleDocument>(this);

            SearchClient client = resources.GetSearchClient();

            Hotel[] data = SearchResources.TestDocuments;

            await using SearchIndexingBufferedSender <Hotel> indexer =
                            client.CreateIndexingBufferedSender <Hotel>();
            AssertNoFailures(indexer);
            try
            {
                await indexer.UploadDocumentsAsync(data);
            }
            catch (InvalidOperationException ex)
            {
                StringAssert.Contains(nameof(Hotel), ex.Message);
            }
        }
示例#26
0
 /// <summary>
 /// Retrieves all active users from the database
 /// </summary>
 /// <returns></returns>
 public userSearchResultTO[] GetActiveUsers()
 {
     userSearchResultTO[] result;
     using (SearchClient client = new SearchClient())
     {
         ConfigureClient(client);
         try
         {
             client.Open();
             result = client.GetActiveUsers();
             client.Close();
         }
         catch (Exception ex)
         {
             client.Abort();
             throw ex;
         }
     }
     return(result);
 }
示例#27
0
        /// <summary>
        /// 同时探索多个节点。适用于文章节点。注意,此函数仅在查询方面进行了优化,
        /// 如果其他线程正在探索此节点,则等待此节点探索完毕。
        /// </summary>
        private async Task FetchPapersAsync(IReadOnlyCollection <KgNode> paperNodes, string constraint)
        {
            Debug.Assert(paperNodes != null);
            if (constraint == null)
            {
                await FetchPapersAsync(paperNodes);

                return;
            }
            // 1. 注意,有些节点可能仍处于 正在探索 的状态。
            //      需要在返回前等待这些正在探索的节点。
            // 2. 很不幸,一旦标记一个节点开始探索后,没有办法再标记为未探索,
            //      所以在有约束的情况下,只能在内层循环中对节点分别标记。
            var nodesToFetch = paperNodes.Where(n => GetStatus(n.Id).GetFetchingStatus(NodeStatus.PaperFetching) == FetchingStatus.Unfetched)
                               .ToArray();

            if (nodesToFetch.Length == 0)
            {
                goto WAIT_FOR_EXPLORATIONS;
            }
            await Task.WhenAll(nodesToFetch.Select(n => n.Id)
                               .Partition(SEB.MaxChainedIdCount)
                               .Select(ids =>
            {
                // 假定 Partition 返回的是 IList / ICollection
                var idc = (ICollection <long>)ids;
                return(SearchClient.EvaluateAsync(SEB.And(SEB.EntityIdIn(idc), constraint),
                                                  SEB.MaxChainedIdCount,
                                                  ConcurrentPagingMode.Optimistic,
                                                  async page =>
                {
                    foreach (var entity in page.Entities)
                    {
                        await ExplorePaperAsync(entity);
                    }
                }));
            }));

WAIT_FOR_EXPLORATIONS:
            ;
        }
示例#28
0
        private async Task FetchCitationsToPaperAsync(PaperNode paper, string constraint, ConcurrentPagingMode pagingMode)
        {
            Debug.Assert(paper != null);
            if (constraint == null)
            {
                if (!await GetStatus(paper.Id).MarkAsFetchingOrUntilFetched(NodeStatus.PaperCitationsFetching))
                {
                    return;
                }
            }
            // 一般来说, Paper2 肯定就是题目中的终结点,
            // 因此我们是知道其具体信息的。
            var maxPapers = CitationCount(paper);
            var strategy  = maxPapers == null
                ? ConcurrentPagingMode.Optimistic
                : pagingMode;

            if (maxPapers == null)
            {
                maxPapers = Assumptions.PaperMaxCitations;
            }
            var expr = SEB.ReferenceIdContains(paper.Id);

            if (constraint != null)
            {
                expr = SEB.And(expr, constraint);
            }
            await SearchClient.EvaluateAsync(expr, maxPapers.Value, strategy, async page =>
            {
                await Task.WhenAll(page.Entities.Select(entity =>
                {
                    RegisterNode(entity);
                    return(ExplorePaperAsync(entity));
                }));
            });

            if (constraint == null)
            {
                GetStatus(paper.Id).MarkAsFetched(NodeStatus.PaperCitationsFetching);
            }
        }
示例#29
0
        /// <summary>
        /// Constructor mainSearch
        /// </summary>
        /// <param name="uriService">endpoint de azure search</param>
        /// <param name="SearchServiceKey">key</param>
        /// <param name="entityIndex">índice del azure search</param>
        public MainSearch(string uriService, string SearchServiceKey, string entityIndex)
        {
            _searchIndex = new SearchIndexClient(new Uri(uriService), new AzureKeyCredential(SearchServiceKey), new SearchClientOptions {
            });



            this.Index = entityIndex;

            this.UriService = uriService;
            this.ServiceKey = SearchServiceKey;

            try
            {
                _searchIndex.GetIndex(entityIndex);
            }
            catch (RequestFailedException exc)
            {
                if (true)
                {
                    if (exc.Status == 404)
                    {
                        CreateOrUpdateIndex();
                    }
                    else
                    {
                        throw exc;
                    }
                }
                ;
            }
            catch (Exception e)
            {
                throw e;
            }



            // cliente azure
            _search = new SearchClient(new Uri(uriService), entityIndex, new AzureKeyCredential(SearchServiceKey));
        }
        public IActionResult Delete(string id)
        {
            SqlClient    sqlClient    = new SqlClient();
            SearchClient searchClient = new SearchClient(
                Environment.GetEnvironmentVariable(ServiceConfiguration.AlgoliaAppId),
                Environment.GetEnvironmentVariable(ServiceConfiguration.AdminKey));
            SearchIndex index = searchClient.InitIndex(ServiceConfiguration.SearchIndex);

            if (Get(id) == null)
            {
                return(NotFound());
            }

            sqlClient.DeleteCompetitiveEvent(
                SqlCommands.DeleteCompetitiveEvent,
                id);
            index.DeleteObject(
                id);

            return(Ok());
        }
示例#31
0
        /// <summary>
        /// Returns true of the web service client is able to successfully connect to the target web service.
        /// </summary>
        /// <returns></returns>
        public bool CheckConnection()
        {
            bool result = false;

            using (SearchClient client = new SearchClient())
            {
                ConfigureClient(client);
                try
                {
                    client.Open();
                    result = client.CheckConnection();
                    client.Close();
                }
                catch (Exception ex)
                {
                    client.Abort();
                    throw ex;
                }
            }
            return(result);
        }
示例#32
0
        public async Task Options()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            #region Snippet:Azure_Search_Tests_Samples_Readme_Options
            int           stars   = 4;
            SearchOptions options = new SearchOptions
            {
                // Filter to only Rating greater than or equal our preference
                //@@ Filter = SearchFilter.Create($"Rating ge {stars}"),
                /*@@*/ Filter = SearchFilter.Create($"rating ge {stars}"),
                Size          = 5, // Take only 5 results
                //@@ OrderBy = { "Rating desc" } // Sort by Rating from high to low
                /*@@*/ OrderBy = { "rating desc" } // Sort by rating from high to low
            };
            SearchResults <Hotel> response = client.Search <Hotel>("luxury", options);
            // ...
            #endregion Snippet:Azure_Search_Tests_Samples_Readme_Options
        }
        public void Query_Sets_LastActivity()
        {
            var context = ContextFactory.GetCouchbaseContext();

            context.SearchUris.Add(new FailureCountingUri("http://10.141.151.101:8091/"));
            var handler = FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{ }")
            });

            var client = new SearchClient(new HttpClient(handler), new SearchDataMapper(), context);

            Assert.IsNull(client.LastActivity);

            client.Query(new SearchQuery
            {
                Index = "index",
                Query = new MatchQuery("foo")
            });
            Assert.IsNotNull(client.LastActivity);
        }
        public async Task StaticDocuments()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            SearchClient          client   = resources.GetQueryClient();
            SearchResults <Hotel> response = await client.SearchAsync <Hotel>("*");

            Assert.IsNull(response.TotalCount);
            Assert.IsNull(response.Coverage);
            Assert.IsNull(response.Facets);

            List <SearchResult <Hotel> > docs = await response.GetResultsAsync().ToListAsync();

            Assert.AreEqual(SearchResources.TestDocuments.Length, docs.Count);
            for (int i = 0; i < docs.Count; i++)
            {
                Assert.AreEqual(1, docs[i].Score);
                Assert.IsNull(docs[i].Highlights);
                Assert.AreEqual(SearchResources.TestDocuments[i], docs[i].Document);
            }
        }
示例#35
0
        static int GetCurrentDocCount(SearchClient searchClient)
        {
            // Get the current doc count of the specified index
            try
            {
                SearchOptions options = new SearchOptions()
                {
                    SearchMode        = SearchMode.All,
                    IncludeTotalCount = true
                };

                SearchResults <Dictionary <string, object> > response = searchClient.Search <Dictionary <string, object> >("*", options);
                return(Convert.ToInt32(response.TotalCount));
            }
            catch (Exception ex)
            {
                Console.WriteLine("  Error: {0}", ex.Message.ToString());
            }

            return(-1);
        }
示例#36
0
            public async Task SupportsSplittingRequests()
            {
                // arrange
                var httpConnection = Substitute.For <IHttpConnection>();

                httpConnection
                .SearchAsync(Arg.Any <Uri>(), Arg.Any <Dictionary <string, object> >())
                .Returns(Task.FromResult(new SearchResult {
                    Results = Array.Empty <ComicVineObject>()
                }));
                var apiConnection = new ApiConnection(httpConnection);
                var client        = new SearchClient(apiConnection);

                // act
                var Search = client.Search("anything")
                             .IncludeResource(SearchResource.Character);
                var SearchA = Search
                              .IncludeResource(SearchResource.Origin);
                var SearchB = Search
                              .IncludeResource(SearchResource.Issue);

                // assert
                var model = await Search.FirstOrDefaultAsync();

                _ = httpConnection.Received().SearchAsync(
                    Arg.Any <Uri>(),
                    Arg.Is <Dictionary <string, object> >(o => o["resources"].Equals("character")));

                var modelA = await SearchA.FirstOrDefaultAsync();

                _ = httpConnection.Received().SearchAsync(
                    Arg.Any <Uri>(),
                    Arg.Is <Dictionary <string, object> >(o => o["resources"].Equals("character,origin")));

                var modelB = await SearchB.FirstOrDefaultAsync();

                _ = httpConnection.Received().SearchAsync(
                    Arg.Any <Uri>(),
                    Arg.Is <Dictionary <string, object> >(o => o["resources"].Equals("character,issue")));
            }
        static void Main(string[] args)
        {
            string serviceName = "<Put your search service NAME here>";
            string apiKey      = "<Put your search service ADMIN API KEY here";
            string indexName   = "hotels-quickstart";

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint            = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential  = new AzureKeyCredential(apiKey);
            SearchIndexClient  adminClient = new SearchIndexClient(serviceEndpoint, credential);

            // Create a SearchClient to load and query documents
            SearchClient srchclient = new SearchClient(serviceEndpoint, indexName, credential);

            // Delete index if it exists
            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(indexName, adminClient);

            // Create index
            Console.WriteLine("{0}", "Creating index...\n");
            CreateIndex(indexName, adminClient);

            SearchClient ingesterClient = adminClient.GetSearchClient(indexName);

            // Load documents
            Console.WriteLine("{0}", "Uploading documents...\n");
            UploadDocuments(ingesterClient);

            // Wait 2 secondsfor indexing to complete before starting queries (for demo and console-app purposes only)
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(2000);

            // Call the RunQueries method to invoke a series of queries
            Console.WriteLine("Starting queries...\n");
            RunQueries(srchclient);

            // End the program
            Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
            Console.ReadKey();
        }
示例#38
0
        public static void RunChemCatCentralSearch(ICswResources CswResources, CswNbtC3SearchReturn Return, CswC3SearchParams CswC3SearchParams)
        {
            JObject         Ret = new JObject();
            CswNbtResources _CswNbtResources = (CswNbtResources)CswResources;

            CswNbtC3ClientManager CswNbtC3ClientManager = new CswNbtC3ClientManager(_CswNbtResources, CswC3SearchParams);
            SearchClient          C3SearchClient        = CswNbtC3ClientManager.initializeC3Client();

            if (null != C3SearchClient)
            {
                CswRetObjSearchResults SearchResults;

                try
                {
                    SearchResults = C3SearchClient.search(CswC3SearchParams);
                }
                catch (TimeoutException TimeoutException)
                {
                    const string WarningMessage = "The search has timed out. Please use more specific search terms.";
                    throw (new CswDniException(CswEnumErrorType.Warning, WarningMessage, WarningMessage, TimeoutException));
                }

                CswNbtWebServiceTable wsTable = new CswNbtWebServiceTable(_CswNbtResources, null, Int32.MinValue);
                Ret["table"]          = wsTable.getTable(SearchResults, CswC3SearchParams.Field, CswNbtC3ClientManager.DataService);
                Ret["filters"]        = "";
                Ret["searchterm"]     = CswC3SearchParams.Query;
                Ret["field"]          = CswC3SearchParams.Field;
                Ret["filtersapplied"] = "";
                Ret["sessiondataid"]  = "";
                Ret["searchtarget"]   = "chemcatcentral";
                Ret["c3dataservice"]  = CswNbtC3ClientManager.DataService;
                Ret["filtered"]       = "C3" == CswNbtC3ClientManager.DataService;
                if (_CswNbtResources.Modules.IsModuleEnabled(CswEnumNbtModuleName.C3ACD))
                {
                    Ret["prefsuppliers"] = CswC3SearchParams.ACDSearchParams.CompanyIds;
                }

                Return.Data.SearchResults = Ret.ToString();
            }
        }
示例#39
0
        static async Task Main(string[] args)
        {
            var searchServiceEndpoint = "https://piasystechbitessearch.search.windows.net";
            var indexName             = "azureblob-index";

            #region queryKey value definition
            var queryKey = "*****";
            #endregion

            SearchClient searchClient = new SearchClient(
                new Uri(searchServiceEndpoint),
                indexName,
                new AzureKeyCredential(queryKey));

            var options = new SearchOptions();

            // You can select fields to retrieve
            options.Select.Add("metadata_storage_name");
            options.Select.Add("metadata_title");
            options.IncludeTotalCount = true;

            // You can sort by
            // options.OrderBy.Add("metadata_title");

            // You can define filters using the OData $filter syntax
            // options.Filter = "title eq 'something'";

            // You can configure paging
            options.Skip = 5;
            options.Size = 10;

            var results = await searchClient.SearchAsync <ResultItem>("italia", options);

            Console.WriteLine($"Total number of results: {results.Value.TotalCount}");

            await foreach (var r in results.Value.GetResultsAsync())
            {
                Console.WriteLine($"Found {r.Document.metadata_storage_name} with score {r.Score}");
            }
        }
        public async Task <int> BuildIndexAsync(IEnumerable <SearchLocationIndex> searchLocations)
        {
            logger.LogInformation($"Starting to build index for {searchLocations.Count()}");
            try
            {
                var searchIndexClient = new SearchIndexClient(azureSearchIndexConfig.EndpointUri, GetAzureKeyCredential());
                var searchClient      = new SearchClient(azureSearchIndexConfig.EndpointUri, azureSearchIndexConfig.LocationSearchIndex, GetAzureKeyCredential());
                var fieldBuilder      = new FieldBuilder();
                var searchFields      = fieldBuilder.Build(typeof(SearchLocationIndex));
                var definition        = new SearchIndex(azureSearchIndexConfig.LocationSearchIndex, searchFields);
                var suggester         = new SearchSuggester(suggestorName, new[] { nameof(SearchLocationIndex.LocationName) });
                definition.Suggesters.Add(suggester);

                logger.LogInformation("created search objects and creating index");
                await searchIndexClient.CreateOrUpdateIndexAsync(definition).ConfigureAwait(false);

                logger.LogInformation("Created search index and uploading documents");

                var batch = IndexDocumentsBatch.Upload(searchLocations);
                IndexDocumentsResult result = await searchClient.IndexDocumentsAsync(batch).ConfigureAwait(false);

                var failedRecords = result.Results.Where(r => !r.Succeeded);
                if (failedRecords.Any())
                {
                    var sampleFailedRecord = failedRecords.FirstOrDefault();
                    var sampleMessage      = $"{failedRecords.Count()} have failed to upload to the index, sample failed record  message {sampleFailedRecord.ErrorMessage}, Status = {sampleFailedRecord.Status}";
                    logger.LogError(sampleMessage);
                    throw new DfcIndexUploadException("sampleMessage");
                }

                logger.LogInformation($"Created search index and uploaded {result.Results.Count} documents");

                return(result.Results.Count);
            }
            catch (Exception ex)
            {
                logger.LogError("Building index had an error", ex);
                throw;
            }
        }
        public async Task TestTimeOut()
        {
            SearchConfig config = new SearchConfig(TestHelper.ApplicationId1, TestHelper.AdminKey1)
            {
                CustomHosts = _hosts
            };

            SearchClient client = new SearchClient(config);

            Stopwatch timer = new Stopwatch();

            timer.Start();

            for (int i = 0; i < 10; i++)
            {
                _ = await client.ListIndicesAsync();
            }

            timer.Stop();

            Assert.IsTrue(timer.ElapsedMilliseconds < 5000);
        }
示例#42
0
        //Create an Azure Cognitive Search index whose schema is compatible with your data source.
        //Create an Azure Cognitive Search data source as described in Create Data Source (Azure Cognitive Search REST API).
        //https://docs.microsoft.com/en-us/rest/api/searchservice/create-data-source

        //Create an Azure Cognitive Search indexer as described in Create Indexer (Azure Cognitive Search REST API).
        //https://docs.microsoft.com/en-us/rest/api/searchservice/create-indexer
        private static void RunQueries(SearchClient srchclient)
        {
            SearchOptions options;
            SearchResults <DocumentSample> response;


            Console.WriteLine("Query #1: Search all documents that include 'New York' (there should be 18)...\n");

            options = new SearchOptions()
            {
                Filter  = "",
                OrderBy = { "" }
            };

            response = srchclient.Search <DocumentSample>("New York", options);
            WriteDocuments(response);
            return;

            Console.WriteLine("Query #2: Find all documents that include 'London' and 'Buckingham Palace' (there should be 2)...\n");

            options = new SearchOptions()
            {
                Filter = "hotelCategory eq 'hotel'",
            };

            response = srchclient.Search <DocumentSample>("*", options);
            WriteDocuments(response);

            Console.WriteLine("Query #3: Filter  all documents that contain the term 'Las Vegas' that have 'reviews' in their URL (there should be 13)...\n");

            options = new SearchOptions()
            {
                Filter  = "baseRate lt 200",
                OrderBy = { "lastRenovationDate desc" }
            };

            response = srchclient.Search <DocumentSample>("*", options);
            WriteDocuments(response);
        }
示例#43
0
        }//getCurrentC3Version()

        public CswC3ServiceLogicGetDataSourcesDataSource[] getDataSourceDates()
        {
            CswC3ServiceLogicGetDataSourcesDataSource[] DataSourceDates = null;
            bool Status = false;

            SearchClient C3Service = initializeC3Client();

            if (null != C3Service)
            {
                Status = _checkConnection(C3Service);
                if (Status)
                {
                    CswRetObjSearchResults DataSources = C3Service.getDataSourceDates(_CswC3Params);
                    if (null != DataSources.DataSourceDates)
                    {
                        DataSourceDates = DataSources.DataSourceDates.Data;
                    }
                }
            }

            return(DataSourceDates);
        }
        public async Task BufferedSender()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            SearchClient searchClient = null;

            try
            {
                searchClient = await CreateIndexAsync(resources);

                // Simple
                {
                    searchClient = GetOriginal(searchClient);

                    #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender1
                    await using SearchIndexingBufferedSender <Product> indexer =
                                    new SearchIndexingBufferedSender <Product>(searchClient);
                    await indexer.UploadDocumentsAsync(GenerateCatalog(count : 100000));

                    #endregion
                }

                await WaitForDocumentCountAsync(searchClient, 100000);

                // Check
                #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender2
#if SNIPPET
                await indexer.FlushAsync();
#endif
                Assert.AreEqual(100000, (int)await searchClient.GetDocumentCountAsync());
                #endregion
            }
            finally
            {
                if (searchClient != null)
                {
                    await resources.GetIndexClient().DeleteIndexAsync(searchClient.IndexName);
                }
            }
        }
示例#45
0
            public void TestingTheOrderParameter()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.SortField = IssueSearchSort.Updated;
                request.Order = SortDirection.Ascending;

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d =>
                        d["sort"] == "updated" &&
                        d["order"] == "asc"));
            }
示例#46
0
            public void TestingTheRepoQualifier()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Repo = "octokit.net";

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+repo:octokit.net"));
            }
示例#47
0
            public void TestingTheCommentsQualifier_Range()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Comments = new Range(10, 20);

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+comments:10..20"));
            }
示例#48
0
 public void TestingTheInQualifier_Email()
 {
     var connection = Substitute.For<IApiConnection>();
     var client = new SearchClient(connection);
     var request = new SearchUsersRequest("github");
     request.In = new[] { UserInQualifier.Email };
     client.SearchUsers(request);
     connection.Received().Get<SearchUsersResult>(
         Arg.Is<Uri>(u => u.ToString() == "search/users"),
         Arg.Is<Dictionary<string, string>>(d => d["q"] == "github+in:Email"));
 }
示例#49
0
            public void TestingTheUpdatedQualifier_LessThanOrEquals()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Updated = DateRange.LessThanOrEquals(new DateTime(2014, 1, 1));

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+updated:<=2014-01-01"));
            }
示例#50
0
            public void TestingTheInQualifiers_Multiple()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.In = new[] { IssueInQualifier.Body, IssueInQualifier.Title };

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+in:body,title"));
            }
示例#51
0
            public void TestingTheLabelsQualifier_Multiple()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Labels = new[] { "bug", "feature" };

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+label:bug+label:feature"));
            }
示例#52
0
            public void TestingTheStateQualifier_Closed()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.State = ItemState.Closed;

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+state:Closed"));
            }
示例#53
0
            public void TestingTheInvolvesQualifier()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Involves = "alfhenrik";

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+involves:alfhenrik"));
            }
示例#54
0
            public void TestingTheSortParameter()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                //get repos where the Description contains rails and user/org is 'github'
                var request = new SearchRepositoriesRequest("rails");
                request.Sort = RepoSearchSort.Forks;

                client.SearchRepo(request);

                connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
            }
示例#55
0
            public void TestingTheTypeQualifier_PR()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Type = IssueTypeQualifier.PR;

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+type:pr"));
            }
示例#56
0
            public void TestingTheSortParameter()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.SortField = IssueSearchSort.Comments;

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d =>
                        d["sort"] == "comments"));
            }
示例#57
0
            public void TestingTheLanguageQualifier()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");
                request.Language = Language.CSharp;

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d => d["q"] == "something+language:CSharp"));
            }
示例#58
0
            public void TestingTheDefaultOrderParameter()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                var request = new SearchIssuesRequest("something");

                client.SearchIssues(request);

                connection.Received().Get<SearchIssuesResult>(
                    Arg.Is<Uri>(u => u.ToString() == "search/issues"),
                    Arg.Is<Dictionary<string, string>>(d =>
                        d["order"] == "desc"));
            }
示例#59
0
            public void TestingTheUpdatedQualifier()
            {
                var connection = Substitute.For<IApiConnection>();
                var client = new SearchClient(connection);
                //get repos where the search contains 'github' and has been pushed before year jan 1 2013
                var request = new SearchRepositoriesRequest("github");
                request.Updated = DateRange.LessThan(new DateTime(2013, 1, 1));

                client.SearchRepo(request);

                connection.Received().Get<SearchRepositoryResult>(Arg.Is<Uri>(u => u.ToString() == "search/repositories"), Arg.Any<Dictionary<string, string>>());
            }
示例#60
0
 public void EnsuresNonNullArguments()
 {
     var client = new SearchClient(Substitute.For<IApiConnection>());
     Assert.Throws<ArgumentNullException>(() => client.SearchIssues(null));
 }