Beispiel #1
3
 public static List<NewsBase> SearchList(string nid)
 {
     string espath = ConfigurationManager.AppSettings["ESPath"].ToString();
     string indexname = ConfigurationManager.AppSettings["IndexName"].ToString();
     string typename = ConfigurationManager.AppSettings["TypeName"].ToString();
     var node = new Uri(espath);
     var settings = new ConnectionSettings(node);
     var client = new ElasticClient(settings);
     //keyword = String.Format("*{0}*", keyword);
     var searchResults = client.Search<NewsBase>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(q => q.QueryString(qs => qs.Query(nid).DefaultOperator(Operator.And)))
         .Sort(st => st.OnField(f => f.newsid).Order(SortOrder.Descending))  /*按ID排序,id为数字,排序正常*/
         //.Sort(st=>st.OnField(f=>f.PubDate.Suffix("sort")).Descending())   /*按时间排序,时间格式,排序bug;中文字符串bug*/
         .From(0)
         .Size(1)
     );
     List<NewsBase> eslist = new List<NewsBase>(searchResults.Documents);
     //foreach (var data in searchResults.Documents)
     //{
     //    eslist.Add(data);
     //}
     return eslist;
 }
Beispiel #2
0
        public void Init()
        {
            var stream = GetValidStream();

            var options = new CredentialProfileOptions
            {
                AccessKey = System.Environment.GetEnvironmentVariable("AWSACCESSKEY"),
                SecretKey = System.Environment.GetEnvironmentVariable("AWSSECRET")
            };

            var profile    = new Amazon.Runtime.CredentialManagement.CredentialProfile("basic_profile", options);
            var netSDKFile = new NetSDKCredentialsFile();
            var region     = Amazon.RegionEndpoint.GetBySystemName(stream.AwsRegion);

            var creds = AWSCredentialsFactory.GetAWSCredentials(profile, netSDKFile);

            var connection = new AwsHttpConnection(creds, region);

            var pool   = new SingleNodeConnectionPool(new Uri(stream.ElasticSearchDomainName));
            var config = new Nest.ConnectionSettings(pool, connection);
            var client = new ElasticClient(config);

            if (client.Indices.Exists(stream.ElasticSearchIndexName).Exists)
            {
                var res = client.Indices.Delete(stream.ElasticSearchIndexName);
                Console.WriteLine(res.DebugInformation);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var indexName          = "geodocument";
            var connectionPool     = new Elasticsearch.Net.SniffingConnectionPool(new Uri[] { new Uri("http://localhost:9200") });
            var connectionSettings = new Nest.ConnectionSettings(connectionPool);

            connectionSettings.DefaultIndex(indexName);
            connectionSettings.DisableDirectStreaming();
            var elasticClient = new ElasticClient(connectionSettings);
            Func <TypeMappingDescriptor <GeoDocument>, ITypeMapping> typeMapping = m => m
                                                                                   .Dynamic(false)
                                                                                   .Properties(ps => ps
                                                                                               .Keyword(k => k
                                                                                                        .Name(n => n.DocId))
                                                                                               .GeoShape(g => g
                                                                                                         .PointsOnly(false)
                                                                                                         .Name(o => o.GeoField)));

            elasticClient.CreateIndex(new CreateIndexDescriptor(indexName).Mappings(ms => ms.Map(typeMapping)));
            var polygon  = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";
            var document = new GeoDocument()
            {
                DocId    = "1",
                GeoField = JsonConvert.DeserializeObject <object>(polygon),
            };
            var indexResponse = elasticClient.IndexDocument(document);

            Console.WriteLine(indexResponse.DebugInformation);

            elasticClient.DeleteIndex(new DeleteIndexRequest(indexName));
            Console.ReadKey();
        }
        private static void ConfigureIdProperty(ConnectionSettings settings)
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            var types = new List<Type>();

            foreach (var assembly in assemblies) {
                try {
                    types.AddRange(assembly.GetTypes());
                }
                catch (ReflectionTypeLoadException exception) {
                    Logger.WriteToLog($"ConfigureIdProperty partialy failed. Assembly={assembly.FullName}",Level.Information,exception);
                }
            }

            types = types.Where(x => typeof(IContent).IsAssignableFrom(x) && !x.IsInterface).ToList();
            settings.MapIdPropertyFor<IContent>(x => x.ContentLink);
            var idProperties = ((IConnectionSettingsValues)settings).IdProperties;

            var name = idProperties[typeof(IContent)];

            foreach (var contentType in types)
            {
                if (!idProperties.ContainsKey(contentType))
                {
                    ((IConnectionSettingsValues)settings).IdProperties.Add(contentType, name);
                }
            }
        }
Beispiel #5
0
        public IEnumerable <DataAccess.DataTransferModels.Elastic.TargetDto> Query()
        {
            var node     = new Uri("http://192.168.1.44:9200");
            var settings = new Nest.ConnectionSettings(node).DefaultIndex("my-replica-set.cpatdb.targets");

            var client = new ElasticClient(settings);
            //var searchResponse = client.Search<DataAccess.DataTransferModels.Elastic.TargetDto>(s => s
            //    .Query(q => q
            //        .Bool(b => b
            //            .Must(m => m
            //                .Match(match => match
            //                    .Field(f => f.Name == query)
            //                )
            //            )
            //        )
            //    )
            //);
            var searchResponse = client.Search <DataAccess.DataTransferModels.Elastic.TargetDto>(s => s
                                                                                                 .Query(q => q.MatchAll())
                                                                                                 );

            var analyticsEntry = new SearchAnalytics()
            {
                Index = "targets", searchDate = DateTime.Now
            };
            var analyticsResponse = client.Index <SearchAnalytics>(analyticsEntry, i => i.Index("analytics"));

            return(searchResponse.Documents.ToList());
        }
Beispiel #6
0
 public BlackListDataManager()
 {
     try
     {
         var elasticSearchUrl = ConfigurationManager.AppSettings["ElasticStoreConnection"];
         var index = ConfigurationManager.AppSettings["EIndex_BlackList"];
         _settings =
             new ConnectionSettings(new Uri(elasticSearchUrl)).SetDefaultIndex(index).PluralizeTypeNames();
         _client = new ElasticClient(_settings);
         var isIndexExist = _client.IndexExists(i => i.Index(index));
         if (!isIndexExist.Exists)
         {
             _client.CreateIndex(c => c.Index(index));
             _settings.SetDefaultIndex(index);
             _client = new ElasticClient(_settings);
         }
         var response =
             _client.Map<BlockedUser>(
                 h =>
                     h.MapFromAttributes(10).Properties(p => p
                         .String(s => s.Name(i => i.ByEmail).Index(FieldIndexOption.NotAnalyzed))
                         .String(s => s.Name(i => i.ToEmail).Index(FieldIndexOption.NotAnalyzed))
                         ));
         IsServerError(response);
     }
     catch (Exception ex)
     {
         Logger.LogException(ex, _soruce, "BlackListDataManager", Severity.Critical);
         throw;
     }
 }
Beispiel #7
0
        public Storage(string serverUrl)
        {
            var passportIndices = new List<string> { ServiceIndex, UserIndex, ClaimIndex, TenantIndex };

            var local = new Uri(serverUrl);
            var settings = new ConnectionSettings(local).DefaultIndex(DefaultIndex);
            _client = new ElasticClient(settings);

            var res = _client.LowLevel.ClusterHealth<object>();

            if (res.SuccessOrKnownError)
            {
                foreach (var index in passportIndices)
                {
                    res = _client.LowLevel.IndicesExists<object>(index);

                    if (res.HttpStatusCode != 200)
                    {
                        Console.WriteLine($"{index} Index does not exist - Initialising");
                        InitialiseIndex(index);
                    }
                    else
                    {
                        Console.WriteLine($"{index} Index is accesible");
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Elastic search server is not reachable");
            }
        }
 public BeerElasticsearch()
 {
     string url = WebConfigurationManager.AppSettings["elasticsearch"];
     this._node = new Uri(url);
     this._settings = new ConnectionSettings(_node, defaultIndex: Setting.ElasticSearchIndex);
     this._client = new ElasticClient(_settings);
 }
		/**== Custom Connection Implementations
		*
		* The client abstracts sending the request and creating a response behind `IConnection`
		*
		* By default the client will use a WebRequest based version on the desktop CLR (.NET 4.5 and up targets)
		* and a HttpClient based HttpConnection specifically build for the Core CLR (netstandard 1.6).
		*
		* The reason for the split is because WebRequest and ServicePoint are not directly available on netstandard 1.6
		*
		* However the implementation written against WebRequest is the most matured implementation that we weren't ready to it give up.
		* There are also a couple of important toggles that are easy to set against a `ServicePoint` that we'd have to give up
		* had we jumped on the `HttpClient` completely.
		*
		* Another limitation is that `HttpClient` has no synchronous code paths and supporting that means doing hacky async patches which definitely
		* need time to bake.
		*
		* So why would you ever want to pass your own `IConnection`? Let's look at a couple of examples
		*
		*/

		public void OverrideHow()
		{
			var connection = new InMemoryConnection();
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var settings = new ConnectionSettings(connectionPool, connection);
			var client = new ElasticClient(settings);
		}
Beispiel #10
0
        public void RebuildIndex(string userId, int? noteId)
        {
            if (!string.IsNullOrWhiteSpace(userId))
            {
                var notes = new List<NoteModel>();

                if (noteId.HasValue)
                    notes.Add(Repository.GetById(noteId.Value));
                else
                    notes = Repository.GetByUserId(userId).ToList();
                

                var settings = new ConnectionSettings(new Uri("http://localhost:9200"), "mapnotes");
                var client = new ElasticClient(settings);


                var indexExists = client.IndexExists("mapnotes");
                if (!indexExists.Exists)
                {
                    client.CreateIndex(descriptor => descriptor.Index("mapnotes")
                            .AddMapping<NoteIndex>(m => m.Properties(p => p.GeoPoint(d => d.Name(f => f.Location).IndexLatLon()))));
                }

                foreach (var note in notes)
                {
                    client.Index(new NoteIndex
                    {
                        Id = note.Id,
                        UserId = note.UserId,
                        Title = note.Title,
                        Location = new Location(note.Latitude, note.Longitude)
                    });
                }
            }
        }
        public IResponse Execute()
        {
            // create client connection
            var node = new Uri(ServerUrl);
            var conn = new ConnectionSettings(node, this.IndexName);
            var client = new ElasticClient(conn);

            // check index name existance
            var existenceResult = client.GetIndex(i => i.Index(IndexName));
            if (existenceResult.ConnectionStatus.Success)
            {
                // delete exist index
                var deleteResult = client.DeleteIndex(i => i.Index(IndexName));
                if (!deleteResult.Acknowledged)
                    return deleteResult;
            }

            // create index
            var createResult = client.CreateIndex(i => i.Index(IndexName));
            if (!createResult.Acknowledged)
                return createResult;

            // set analyzer
            SetAnalyzers(client);

            // put mapping
            var putResult = client.Map<TranslationMemory>(m => m.Index(this.IndexName).MapFromAttributes());
            //var putResult = client.Map<ElasticSearchProject>(m => m.Index(this.IndexName));
            return putResult;
        }
        public async Task<IEnumerable<Message>> Get(string q, int from, int size)
        {
            var url = ConfigurationManager.AppSettings["ES_URL"];
            var setting = new ConnectionSettings(new Uri(url));
            var client = new ElasticClient(setting);

            // Parse the search query
            string[] terms = q.Split(' ');
            var personTerm = terms.SingleOrDefault(x => x.StartsWith("person:"));
            if (!string.IsNullOrEmpty(personTerm))
            {
                terms = terms.Except(new string[] { personTerm }).ToArray();
                personTerm = personTerm.Replace("person:", string.Empty);
            }
            var textTerms = string.Join(" ", terms);

            var searchResults = await client.SearchAsync<Message>(s => s
                .AllIndices()
                .AllTypes()
                .Size(size)
                .From(from)
                .SortAscending(f => f.Date)
                .Query(qry =>
                    (qry.Term("from", personTerm) ||
                    qry.Term("to", personTerm)) &&
                    qry.Match(m => m
                        .OnField(f => f.Text)
                        .Query(textTerms)
                        .Operator(Operator.And))));

            return searchResults.Documents;
        }
Beispiel #13
0
        public void PopulateFromConnections(IEnumerable<string> settingNodes)
        {
            string lastErrorMessage = null;
            Exception lastException = null;
            foreach (var n in settingNodes)
            {
                var uri = new Uri(string.Format("http://{0}", n));
                var cs = new ConnectionSettings(uri).SetTimeout(2000);
                var profiledConn = new ProfiledElasticConnection(cs);
                var cli = new ElasticClient(cs, profiledConn);
                IResponse response = RefreshFromConnection(cli);

                // Some implementations are raw
                if (response == null) return;

                // All's well with the world
                if (response.IsValid) return;

                if (response.ConnectionStatus.Error == null) continue;
                lastErrorMessage = response.ConnectionStatus.Error.ExceptionMessage;
                lastException = response.ConnectionStatus.Error.OriginalException;
            }
            // 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);
        }
        /// <summary>
        /// Provides base <see cref="ElasticClient"/> with profiling features to current <see cref="MiniProfiler"/> session.
        /// </summary>
        /// <param name="configuration">Instance of <see cref="ConnectionSettings"/>. Its responses will be handled and pushed to <see cref="MiniProfiler"/></param>
		public ProfiledElasticClient(ConnectionSettings configuration)
			: base(configuration)
		{
			ProfilerUtils.ExcludeElasticsearchAssemblies();
			ProfilerUtils.ApplyConfigurationSettings(configuration);
			configuration.SetConnectionStatusHandler(response => MiniProfilerElasticsearch.HandleResponse(response, _profiler));
		}
        static void Main(string[] args)
        {
            var context = new ElasticDBEntities();

            var artists = context.Artists.ToList();

            var node = "http://localhost:9200";

            var searchBoxUri = new Uri(node);
            var settings = new ConnectionSettings(searchBoxUri);
            //settings.SetDefaultIndex("sample");

            var client = new ElasticClient(settings);
            
            if (client.IndexExists("store").Exists)
            {
                client.DeleteIndex("store");
            }

            //client.CreateIndex("sample");

            foreach (var artist in artists)
            {
                //var index = client.Index(artist);
                var index = client.Index(artist, i => i.Index("store").Refresh());
            }

            // Index all documents
            //client.IndexMany<Artist>(artists);

        }
Beispiel #16
0
		public static ConnectionSettings CreateSettings(Func<ConnectionSettings, ConnectionSettings> modifySettings = null, int port = 9200, bool forceInMemory = false)
		{
			var defaultSettings = new ConnectionSettings(new SingleNodeConnectionPool(CreateNode(port)), CreateConnection(forceInMemory: forceInMemory))
				.SetDefaultIndex("default-index")
				.PrettyJson()
				.InferMappingFor<Project>(map => map
					.IndexName("project")
					.IdProperty(p => p.Name)
				)
				.InferMappingFor<CommitActivity>(map => map
					.IndexName("project")
					.TypeName("commits")
				)
				.InferMappingFor<Developer>(map => map
					.IndexName("devs")
					.Ignore(p => p.PrivateValue)
					.Rename(p => p.OnlineHandle, "nickname")
				)
				//We try and fetch the test name during integration tests when running fiddler to send the name 
				//as the TestMethod header, this allows us to quickly identify which test sent which request
				.SetGlobalHeaders(new NameValueCollection
				{
					{ "TestMethod", ExpensiveTestNameForIntegrationTests() }
				});

			var settings = modifySettings != null ? modifySettings(defaultSettings) : defaultSettings;
			return settings;
		}
Beispiel #17
0
 public GameDataManager()
 {
     try
     {
         var elasticSearchUrl = ConfigurationManager.AppSettings["ElasticStoreConnection"];
         var index = ConfigurationManager.AppSettings["EIndex_Game"];
         _settings =
             new ConnectionSettings(new Uri(elasticSearchUrl)).SetDefaultIndex(index).PluralizeTypeNames();
         _client = new ElasticClient(_settings);
         var isIndexExist = _client.IndexExists(i => i.Index(index));
         if (!isIndexExist.Exists)
         {
             _client.CreateIndex(c => c.Index(index));
             _settings.SetDefaultIndex(index);
             _client = new ElasticClient(_settings);
         }
         var response =
             _client.Map<Game>(
                 h =>
                     h.Properties(p => p
                         .String(s => s.Name(i => i.GameId).Index(FieldIndexOption.NotAnalyzed))
                         .String(s => s.Name(i => i.Status).Index(FieldIndexOption.NotAnalyzed))
                         .String(s => s.Name(i => i.TurnOfPlayer).Index(FieldIndexOption.NotAnalyzed))
                         .Object<Winner>(o => o.Name(w => w.Winner).Properties(wp => wp.String(f => f.Name(l => l.FacebookId).Index(FieldIndexOption.NotAnalyzed)))
                         )));
         IsServerError(response);
     }
     catch (Exception ex)
     {
         Logger.LogException(ex, _source, "GameDataManager", Severity.Critical);
         throw;
     }
 }
Beispiel #18
0
 public ElasticRepo()
 {
     var node = new Uri("http://localhost:9200");
     var settings = new ConnectionSettings(node, defaultIndex: "restaurant");
     var client = new ElasticClient(settings);
     _Client = client;
 }
        /// <summary>
        /// C/tor
        /// </summary>
        /// <param name="namedLock">The name of the lock to create. REQUIRED</param>
        /// <param name="client">An instance of an elastic search NEST client. Will be created automatically by default. Use this if connecting to an elastic cluster.</param>
        /// <param name="indexName">The name of the elastic search index. Default = distributedlocks</param>
        public ElasticSearchDistributedLock(string namedLock, IElasticClient client = null, string indexName = "distributedlocks")
        {
            if (string.IsNullOrEmpty(namedLock))
                throw new ArgumentException("namedLock cannot be null or empty");

            mIndex = indexName;
            Name = namedLock;
            mStopwatch = new Stopwatch();

            //Only do this once per process.. 
            if (string.IsNullOrEmpty(mOwner))
            {
                mOwner = BuildOwnerIdentifier();
            }

            //Create a default client if none handed in
            if (client == null)
            {                
                var settings = new ConnectionSettings(defaultIndex: mIndex);
                mClient = new ElasticClient(settings);
            }
            else
            {
                mClient = client;
            }            
        }
Beispiel #20
0
 public void construct_client_with_invalid_hostname()
 {
     Assert.Throws<UriFormatException>(() =>
     {
         var settings = new ConnectionSettings("some mangled hostname", 80);
     });
 }
 public void Initialize(string uri = "http://localhost:9200/", bool deleteIndex = false)
 {
     _logger.Info("Initialing elastic search with uri: " + uri);
     var connectionString = new ConnectionSettings(
         new Uri(uri))
         .DefaultIndex(OSM_NAMES_INDEX)
         .PrettyJson();
     _elasticClient = new ElasticClient(connectionString);
     if (deleteIndex && _elasticClient.IndexExists(OSM_NAMES_INDEX).Exists)
     {
         _elasticClient.DeleteIndex(OSM_NAMES_INDEX);
     }
     if (deleteIndex && _elasticClient.IndexExists(OSM_HIGHWAYS_INDEX).Exists)
     {
         _elasticClient.DeleteIndex(OSM_HIGHWAYS_INDEX);
     }
     _elasticClient.CreateIndex(OSM_HIGHWAYS_INDEX,
             c => c.Mappings(
                 ms => ms.Map<object>(m =>
                     m.Properties(ps => ps.GeoShape(g => g.Name("geometry")
                         .Tree(GeoTree.Geohash)
                         .TreeLevels(10)
                         .DistanceErrorPercentage(0.2))))));
     _logger.Info("Finished initialing elastic search with uri: " + uri);
 }
        public PostIndexer()
        {
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(node);

            _client = new ElasticClient(settings);
        }
Beispiel #23
0
        public void TestIndexTimeout()
        {
            var timeout = 1;
            var s = new ConnectionSettings(Test.Default.Host, Test.Default.Port, timeout)
                        .SetDefaultIndex(Test.Default.DefaultIndex)
                        .SetMaximumAsyncConnections(Test.Default.MaximumAsyncConnections)
                        .UsePrettyResponses();

            var client = new ElasticClient(s);

            var newProject = new ElasticSearchProject
            {
                Name = "COBOLES", //COBOL ES client ?
            };
            var t = client.IndexAsync<ElasticSearchProject>(newProject);
            t.Wait(1000);
            var cs = t.Result;
            Assert.False(cs.Success);
            Assert.NotNull(cs.Error);
            Assert.NotNull(cs.Error.OriginalException);
            Trace.WriteLine(cs.Error.OriginalException);
            Assert.IsNotNullOrEmpty(cs.Error.ExceptionMessage);
            Assert.IsTrue(cs.Error.OriginalException is WebException);
            var we = cs.Error.OriginalException as WebException;
            Assert.IsTrue(cs.Error.ExceptionMessage.Contains("The request was canceled"));
            Assert.IsTrue(we.Status == WebExceptionStatus.RequestCanceled);
            Assert.True(t.IsCompleted, "task did not complete");
            Assert.False(t.IsFaulted, "task was faulted, wich means the exception did not cleanly pass to ConnectionStatus");
        }
        public ElasticClient Client()
        {
            var node = new Uri("http://10.128.36.197:9200/");
            var settings = new ConnectionSettings(node, defaultIndex: "disk");

            return new ElasticClient(settings);
        }
 public static IElasticClient GetClient()
 {
     var node = new Uri("http://localhost:9200");
     var settings = new ConnectionSettings(node);
     settings.DefaultIndex("stackoverflow");
     return new ElasticClient(settings);
 }
Beispiel #26
0
        public async Task Example()
        {
            Environment.SetEnvironmentVariable("EVENTSTOREURL", "tcp://*****:*****@"http://localhost:9200"; //Environment.GetEnvironmentVariable("ELASTICSEARCHURL");
            Uri    node             = new Uri(elasticSearchUrl);

            Nest.ConnectionSettings settings = new Nest.ConnectionSettings(node);
            settings.DisableDirectStreaming();
            ElasticClient elasticClient = new ElasticClient(settings);

            // We wire up EventFlow with all of our classes. Instead of adding events,
            // commands, etc. explicitly, we could have used the the simpler
            // AddDefaults(Assembly) instead.
            using (var resolver = EventFlowOptions.New
                                  .AddEvents(typeof(ExampleEvent))
                                  .AddCommands(typeof(ExampleCommand), typeof(ExampleUpdateCommand))
                                  .AddCommandHandlers(typeof(ExampleCommandHandler), typeof(ExampleUpdateCommandHandler))
                                  .ConfigureEventStorePersistence("tcp://localhost:1113")
                                  .ConfigureElasticsearch(() => elasticClient)
                                  .UseElasticsearchReadModel <ExampleReadModel>()
                                  //.UseInMemoryReadStoreFor<ExampleReadModel>()
                                  .CreateResolver())
            {
                for (int i = 0; i < 10; i++)
                {
                    var rndMagicNum = new Random().Next();

                    // Create a new identity for our aggregate root
                    var exampleId = ExampleId.New;

                    // Resolve the command bus and use it to publish a command
                    var commandBus = resolver.Resolve <ICommandBus>();
                    await commandBus.PublishAsync(
                        new ExampleCommand(exampleId, rndMagicNum), CancellationToken.None)
                    .ConfigureAwait(false);


                    await commandBus.PublishAsync(
                        new ExampleUpdateCommand(exampleId, 1), CancellationToken.None)
                    .ConfigureAwait(false);


                    // Resolve the query handler and use the built-in query for fetching
                    // read models by identity to get our read model representing the
                    // state of our aggregate root
                    var queryProcessor   = resolver.Resolve <IQueryProcessor>();
                    var exampleReadModel = await queryProcessor.ProcessAsync(
                        new ReadModelByIdQuery <ExampleReadModel>(exampleId), CancellationToken.None)
                                           .ConfigureAwait(false);

                    // Verify that the read model has the expected magic number
                    exampleReadModel.MagicNumber.Should().Be(1);

                    Output.WriteLine(exampleId.Value);
                }
            }
        }
        private IConnectionSettingsValues InitialiseConnectionSettings()
        {
            var config = _configProvider.Get <ElasticConfig>();

            var connectionSettings = new Nest
                                     .ConnectionSettings(new SingleNodeConnectionPool(new Uri(config.Url)))
                                     .ThrowExceptions();

            connectionSettings.BasicAuthentication(config.UserName, config.Password);

            return(connectionSettings);
        }
Beispiel #28
0
        protected static ElasticClient GetESClient(PluginConfig pluginConfig)
        {
            using (ConnectionSettings connection = new Nest.ConnectionSettings(new Uri(pluginConfig.ElasticsearchUrl)))
            {
                if (pluginConfig.SecurityType == "basic" && pluginConfig.Username != null && pluginConfig.Password != null)
                {
                    connection.BasicAuthentication(pluginConfig.Username, pluginConfig.Password);
                }

                connection.DefaultIndex("active-homeseer-index");

                ElasticClient client = new Nest.ElasticClient(connection);
                return(client);
            }
        }
        public static ElasticClient CreateEsClient(string host, string indexName)
        {
            var uri  = new Uri(host);
            var pool = new Elasticsearch.Net.SingleNodeConnectionPool(uri);

            var transportsConnectionSettings = new Nest.ConnectionSettings(pool, new Elasticsearch.Net.HttpConnection(), new SerializerFactory((settings, values) =>
            {
                //settings.Converters.Add(new GeometryConverter());
                //settings.Converters.Add(new CoordinateConverter());
            }))
                                               .DefaultIndex(indexName)
                                               .DisableDirectStreaming();

            return(new Nest.ElasticClient(transportsConnectionSettings));
        }
        public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
        {
            var url          = configuration["elasticsearch:url"];
            var defaultIndex = configuration["elasticsearch:index"];

            var settings = new Nest.ConnectionSettings(new Uri(url))
                           .DefaultIndex(defaultIndex)
                           .DefaultMappingFor <News>(m => m
                                                     .PropertyName(p => p.Id, "id")
                                                     );

            var client = new ElasticClient(settings);

            services.AddSingleton <IElasticClient>(client);
        }
Beispiel #31
0
        public string ListIndices()
        {
            var node     = new Uri("http://192.168.1.44:9200");
            var settings = new Nest.ConnectionSettings(node).DefaultIndex("my-replica-set.cpatdb.targets");

            var client         = new ElasticClient(settings);
            var searchResponse = client.Search <DataAccess.DataTransferModels.Elastic.TargetDto>(s => s
                                                                                                 .Query(q => q.MatchAll())
                                                                                                 );

            // just using this as a test for now

            // Call to Elastic, get a list of the indices.

            return("this is a test");
        }
Beispiel #32
0
        public DataAccess(ICsvReader csvReader, IConfiguration configuration)
        {
            this.csvReader     = csvReader;
            this.configuration = configuration;
            var uri  = new Uri(configuration.GetSection("Elastic").GetSection("Host").Value);
            var pool = new Elasticsearch.Net.SingleNodeConnectionPool(uri);

            var transportsConnectionSettings = new Nest.ConnectionSettings(pool, new Elasticsearch.Net.HttpConnection(), new SerializerFactory((settings, values) =>
            {
                //settings.Converters.Add(new GeometryConverter());
                //settings.Converters.Add(new CoordinateConverter());
            }))
                                               .DefaultIndex(configuration.GetSection("Elastic").GetSection("Index").Value)
                                               .DisableDirectStreaming();

            airportElasticClient = new Nest.ElasticClient(transportsConnectionSettings);
        }
Beispiel #33
0
        public static void AddElasticsearchClient(this IServiceCollection services, IConfiguration configuration, string mappingAssemblyName)
        {
            var url          = configuration[MicroserviceConfigurationVariables.ElasticSearch.SERVER];
            var defaultIndex = configuration[MicroserviceConfigurationVariables.ElasticSearch.INDEX];
            var user         = configuration[MicroserviceConfigurationVariables.ElasticSearch.USERNAME];
            var password     = configuration[MicroserviceConfigurationVariables.ElasticSearch.PASSWORD];

            var settings = new ConnectionSettings(new Uri(url))
                           .DefaultIndex(defaultIndex)
                           .BasicAuthentication(user, password)
                           .ThrowExceptions();

            var client = new ElasticClient(settings);

            AddElasticsearchMappings(client, mappingAssemblyName);
            services.AddSingleton(client);
        }
Beispiel #34
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();

            services.AddTransient <ISearchIndexBuilder, SearchIndexBuilder>();
            services.AddTransient <IBoardGameCsvRowMapper, BoardGameCsvRowMapper>();
            services.AddTransient <IBoardGameCsvReader, BoardGameCsvReader>();
            services.AddTransient <ISearchResultsMapper, SearchResultsMapper>();
            services.AddTransient <ISearcher, Searcher>();
            services.AddTransient <IElasticClient>(s =>
            {
                var node     = new Uri("http://localhost:9201");
                var settings = new Nest.ConnectionSettings(node);
                settings.DefaultIndex("games");
                return(new ElasticClient(settings));
            });
        }
        private static IElasticClient GetElastic()
        {
            var url      = Configuration["ElasticConnection"];
            var user     = Configuration["ElasticUserName"];
            var password = Configuration["ElasticPassword"];

            var node = new Uri(url);
            var pool = new SingleNodeConnectionPool(node);

            var regex    = new Regex("([+\\-!\\(\\){}\\[\\]^\"~*?:\\\\\\/>< ]|[&\\|]{2}|AND|OR|NOT)", RegexOptions.Compiled);
            var settings = new Nest.ConnectionSettings(pool, (builtin, param) => new JsonNetSerializer(builtin, param))
                           .BasicAuthentication(user, password)
                           .DefaultIndex("eShop")
                           .DefaultFieldNameInferrer(field => regex.Replace(field, ""))
                           .DisableDirectStreaming();

            return(new ElasticClient(settings));
        }
        public void Init()
        {
            var stream     = GetValidStream();
            var connection = new AwsHttpConnection(stream.AWSRegion, new StaticCredentialsProvider(new AwsCredentials
            {
                AccessKey = stream.AWSAccessKey,
                SecretKey = stream.AWSSecretKey
            }));

            var pool   = new SingleNodeConnectionPool(new Uri(stream.ESDomainName));
            var config = new Nest.ConnectionSettings(pool, connection);
            var client = new ElasticClient(config);

            if (client.IndexExists(stream.ESIndexName).Exists)
            {
                var res = client.DeleteIndex(stream.ESIndexName);
                Console.WriteLine(res.DebugInformation);
            }
        }
Beispiel #37
0
        protected void lnkbtnImportFile_Click(object sender, EventArgs e)
        {
            int       success     = 0;
            string    APP_PATH    = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            DataTable dtProdOrder = new DataTable();

            if (fileprodOrder.HasFile)
            {
                string fileName = fileprodOrder.FileName;
                if (Path.GetExtension(fileName) != ".xlsx")
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "RecordsTextopenModaladded", "openWarningModal('Please choose a valid excel(.xlsx) file.');", true);
                    return;
                }
                else
                {
                    string savedFileName = "";
                    if (!Directory.Exists(Server.MapPath("ImportedFiles")))
                    {
                        Directory.CreateDirectory(Server.MapPath("ImportedFiles"));
                        savedFileName = Server.MapPath("ImportedFiles//" + fileName);
                    }
                    else
                    {
                        savedFileName = Server.MapPath("ImportedFiles//" + fileName);
                    }

                    fileprodOrder.SaveAs(savedFileName);
                    List <TroubleshootingData1> list = GetDataTableFromFile(savedFileName);
                    if (list.Count > 0)
                    {
                        var uris              = new Uri(ConfigurationManager.AppSettings["elasticsearchConnString"].ToString());
                        var settings          = new Nest.ConnectionSettings(uris);
                        var client            = new ElasticClient(settings);
                        var bulkIndexResponse = client.Bulk(b => b
                                                            .Index("servicereport2")
                                                            .IndexMany(list)
                                                            );
                    }
                }
            }
        }
Beispiel #38
0
        private IBulkResponse IndexNews(IEnumerable <NewsArticle> newsArticles)
        {
            if (newsArticles == null || !newsArticles.Any())
            {
                return(null);
            }

            // Connecting to Elasticsearch
            string protocol = Settings.GetSetting("ElasticSearch.Protocol", "http");
            string host     = Settings.GetSetting("ElasticSearch.Host", "elastic.local");
            string port     = Settings.GetSetting("ElasticSearch.Port", "9200");

            var node     = new Uri(string.Format("{0}://{1}:{2}", protocol, host, port));
            var settings = new Nest.ConnectionSettings(node);
            var client   = new Nest.ElasticClient(settings);

            // Reindexing items
            var indexName = Settings.GetSetting("ElasticSearch.ArticlesIndex", "articles-index");

            var indexerResponse = client.IndexMany(newsArticles, indexName);

            return(indexerResponse);
        }
Beispiel #39
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .AddNewtonsoftJson();
            services.AddSingleton(_ => {
                var connection = EventStoreConnection.Create("ConnectTo=tcp://admin:changeit@localhost:1113");

                connection.ConnectAsync().Wait();

                return(connection);
            });
            services.AddTransient <IPostRepository, PostRepository>();
            services.AddTransient <IPostQueries, PostQueries>();
            services.AddMediatR(Assembly.GetExecutingAssembly());
            services.AddSingleton(_ =>
            {
                var settings = new Nest.ConnectionSettings(new Uri("http://localhost:9200"))
                               .DefaultMappingFor <Application.Models.Post>(m => m
                                                                            .IndexName("posts")
                                                                            );

                return(new ElasticClient(settings));
            });
        }
Beispiel #40
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            services.AddScoped <IOrcestratorService, OrchestratorService>();
            services.AddScoped <IAmqService, AmqService>();
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            services.AddScoped <ILogService, LogService>();
            services.AddScoped <IApiCall, ApiCall>();
            services.AddDbContext <MedAppDbContext>(options => options.UseSqlServer("Server = localhost, 1433; Database = TimeScheduel; User ID = SA; Password = Passw0rd;"));
            services.AddHostedService <Worker>();

            var settings = new Nest.ConnectionSettings(new Uri("http://elasticsearch:9200"));

            services.AddSingleton(settings);

            services.AddScoped(s =>
            {
                var connectionSettings = s.GetRequiredService <Nest.ConnectionSettings>();
                var client             = new ElasticClient(connectionSettings);

                return(client);
            });
        });
        public static IElasticClient ConfigureElastic()
        {
            Logger.Info("Setting up Elastic");
            var connectionString = ConfigurationManager.ConnectionStrings["Elastic"];

            if (connectionString == null)
            {
                throw new ArgumentException("No elastic connection string found");
            }

            var data = connectionString.ConnectionString.Split(';');

            var url = data.FirstOrDefault(x => x.StartsWith("Url", StringComparison.CurrentCultureIgnoreCase));

            if (url == null)
            {
                throw new ArgumentException("No URL parameter in elastic connection string");
            }
            var index = data.FirstOrDefault(x => x.StartsWith("DefaultIndex", StringComparison.CurrentCultureIgnoreCase));

            if (string.IsNullOrEmpty(index))
            {
                throw new ArgumentException("No DefaultIndex parameter in elastic connection string");
            }

            index = index.Substring(13);

            var node = new Uri(url.Substring(4));
            var pool = new Elasticsearch.Net.SingleNodeConnectionPool(node);

            var regex    = new Regex("([+\\-!\\(\\){}\\[\\]^\"~*?:\\\\\\/>< ]|[&\\|]{2}|AND|OR|NOT)", RegexOptions.Compiled);
            var settings = new Nest.ConnectionSettings(pool, (_) => new JsonNetSerializer(_))
                           .DefaultIndex(index)
                           .DefaultTypeNameInferrer(type =>
                                                    type.FullName
                                                    .Replace("Pulse.Presentation.ServiceStack.", "")
                                                    .Replace("Pulse.Application.Elastic.", "")
                                                    .Replace('.', '_')
                                                    )
                           .DefaultFieldNameInferrer(field => regex.Replace(field, ""));

#if DEBUG
            settings = settings.DisableDirectStreaming();
#endif

            var client = new ElasticClient(settings);


            if (!client.IndexExists(index).Exists)
            {
                client.CreateIndex(index, i => i
                                   .Settings(s => s
                                             .NumberOfShards(8)
                                             .NumberOfReplicas(0)
                                             .Analysis(analysis => analysis
                                                       .TokenFilters(f => f.NGram("ngram", d => d.MinGram(1).MaxGram(15)))
                                                       .Analyzers(a => a
                                                                  .Custom("default_index", t => t.Tokenizer("keyword").Filters(new[] { "standard", "lowercase", "asciifolding", "kstem", "ngram" }))
                                                                  .Custom("suffix", t => t.Tokenizer("keyword").Filters(new[] { "standard", "lowercase", "asciifolding", "reverse" }))
                                                                  )
                                                       )));
            }

            return(client);
        }
Beispiel #42
0
        public IElasticClient ConfigureElastic()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["Elastic"];

            if (connectionString == null)
            {
                throw new ArgumentException("No elastic connection string found");
            }

            var data = connectionString.ConnectionString.Split(';');

            var url = data.FirstOrDefault(x => x.StartsWith("Url", StringComparison.CurrentCultureIgnoreCase));

            if (url == null)
            {
                throw new ArgumentException("No URL parameter in elastic connection string");
            }
            var index = data.FirstOrDefault(x => x.StartsWith("DefaultIndex", StringComparison.CurrentCultureIgnoreCase));

            if (index.IsNullOrEmpty())
            {
                throw new ArgumentException("No DefaultIndex parameter in elastic connection string");
            }

            index = index.Substring(13);

            var node = new Uri(url.Substring(4));

            var settings = new Nest.ConnectionSettings(node);

            settings.SetDefaultIndex(index);

            settings.SetDefaultTypeNameInferrer(type => type.FullName.Replace("Demo.Application.ServiceStack.", "").Replace('.', '_'));
            // Disable camel case field names (need to match out POCO field names)
            settings.SetDefaultPropertyNameInferrer(field => field);

            var client = new ElasticClient(settings);

            if (!client.IndexExists(index).Exists)
            {
                client.CreateIndex(index, i => i
                                   .Analysis(analysis => analysis
                                             .TokenFilters(f => f
                                                           .Add("ngram", new Nest.NgramTokenFilter {
                    MinGram = 2, MaxGram = 15
                })
                                                           )
                                             .Analyzers(a => a
                                                        .Add(
                                                            "default_index",
                                                            new Nest.CustomAnalyzer
                {
                    Tokenizer = "standard",
                    Filter    = new[] { "standard", "lowercase", "asciifolding", "kstem", "ngram" }
                }
                                                            )
                                                        .Add(
                                                            "suffix",
                                                            new Nest.CustomAnalyzer
                {
                    Tokenizer = "keyword",
                    Filter    = new[] { "standard", "lowercase", "asciifolding", "reverse" }
                }
                                                            )
                                                        )
                                             ));
            }

            return(client);
        }
 private SearchAdapter()
 {
     esconnSetting = new ConnectionSettings(
                         new Uri(ESConnectionSetting.Node), "lt028310");
     esClient = new ElasticClient(esconnSetting);
     
 }
Beispiel #44
-1
        static void Main(string[] args)
        {
            elasticSettings = new ConnectionSettings(new Uri("http://127.0.0.1:9200"))
                .SetDefaultIndex("people");

            client = new ElasticClient(elasticSettings);

            ///// ** Basic search with QueryString
            var searchResults = client.Search<Person>(s => s.Query(q => q.
                                                    QueryString(x => x.Query("toto").
                                                                        OnField(of => of.LastName))));

            foreach (var result in searchResults.Documents)
            {
                Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            }

            ///// ** Basic search on Message
            //var searchResults = client.Search<Person>(s => s.Query(q => q.
            //    QueryString(x => x.Query("brother").
            //        OnField(of => of.Message).
            //        Operator((Operator.and)))));

            //foreach (var result in searchResults.Documents)
            //{
            //    Console.WriteLine("Found: {0}", result.FirstName + " " + result.LastName);
            //}

            Console.ReadKey();
        }
		[U] public void SearchDoesNotTakeDefaultIndexIntoAccount()
		{
			var node = new Uri("http://localhost:9200");
			var connectionPool = new SingleNodeConnectionPool(node);
			var connectionSettings = new ConnectionSettings(connectionPool, connection: new InMemoryConnection())
				.DefaultIndex("logstash-*")
				.DefaultFieldNameInferrer(p => p)
				.OnRequestCompleted(info =>
				{
					// info.Uri is /_search/ without the default index
					// my ES instance throws an error on the .kibana index (@timestamp field not mapped because I sort on @timestamp)
				});

			var client = new ElasticClient(connectionSettings);
			var response = client.Search<ESLogEvent>(s=>s);

			response.ApiCall.Uri.AbsolutePath.Should().Be("/logstash-%2A/eslogevent/_search");

			response = client.Search<ESLogEvent>(new SearchRequest<ESLogEvent>{ });
			response.ApiCall.Uri.AbsolutePath.Should().Be("/logstash-%2A/eslogevent/_search");

			response = client.Search<ESLogEvent>(new SearchRequest { });
			response.ApiCall.Uri.AbsolutePath.Should().Be("/_search");

		}
Beispiel #46
-1
        public static void Main(string[] args)
        {
            var node = new Uri("http://localhost:9200");

            var settings = new ConnectionSettings(node, "elastic-searcher");
            settings.SetDefaultTypeNameInferrer(t => t.Name.ToUpperInvariant());

            IElasticClient client = new ElasticClient(settings);
            IPersonSearch personSearcher = new PersonSearch(client);

            Utils.IndexItems(client);
            var value = Console.ReadLine();
            int id = Convert.ToInt32(value);

            if (id != 0)
            {
                personSearcher.GetItem<Person>(id);
            }
            else
            {
                Utils.ClearItems(client);
            }

            Console.ReadKey();
        }
        public ActionResult ReIndexAll()
        {
            var documents = db.Documents.ToList();

            var uriString = ConfigurationManager.AppSettings["SEARCHBOX_URL"];
            var searchBoxUri = new Uri(uriString);

            var settings = new ConnectionSettings(searchBoxUri);
            settings.SetDefaultIndex("sample");

            var client = new ElasticClient(settings);

            // delete index if exists at startup
            if (client.IndexExists("sample").Exists)
            {
                client.DeleteIndex("sample");
            }

            // Create a new "sample" index with default settings
            client.CreateIndex("sample", new IndexSettings());

            // Index all documents
            client.IndexMany<Document>(documents);

            ViewBag.Message = "Reindexing all database is complete!";

            return RedirectToAction("Index");
        }
Beispiel #48
-1
        public void Run(string indexName, int port, int numMessages, int bufferSize)
        {
            var bulkParms = new SimpleBulkParameters() { Refresh = false };

            var settings = new ConnectionSettings("localhost", port)
                .SetDefaultIndex(indexName);

            var client = new ElasticClient(settings, new ThriftConnection(settings));

            Connect(client, settings);

            IList<Message> msgBuffer = new List<Message>(bufferSize);

            var msgGenerator = new MessageGenerator();

            foreach (var msg in msgGenerator.Generate(numMessages))
            {
                msgBuffer.Add(msg);

                // Flush buffer once max size reached
                if (msgBuffer.Count >= bufferSize)
                {
                    client.IndexMany(msgBuffer, indexName, bulkParms);
                    msgBuffer.Clear();

                    Interlocked.Add(ref NumSent, bufferSize);

                    // Output how many messages sent so far
                    if (NumSent % 10000 == 0)
                    {
                        Console.WriteLine("Sent {0:0,0} messages over Thrift", NumSent);
                    }
                }
            }
        }
        // GET: Search
        public ActionResult Index(string r)
        {
            var uri = new Uri("https://IlwEAOgvDkuHk3yiB74RhwSs1YC0KCUu:@aniknaemm.east-us.azr.facetflow.io");
            var settings = new ConnectionSettings(uri).SetDefaultIndex("indexdem");
            var client = new ElasticClient(settings);

            var result = client.Search<Demotivator>(q => q
            .Query(f => f
               .QueryString(t => t.Query(r + "*").OnFields(u => u.Name)) || f
               .QueryString(t => t.Query(r + "*").OnFields(u => u.Str1)))

            );
            SearchViewModel model = new SearchViewModel();
            List<Demotivator> tr = new List<Demotivator>();

            foreach (var t in result.Hits)
            {

                var sleep = (Demotivator)t.Source;
                int temp = new int();

                if (sleep != null)
                {

                    tr.Add(sleep);
                }
                else
                {
                }

            }
            model.demotivators = tr;
            return View(model);
        }
Beispiel #50
-1
 public LogstashRepo()
 {
     var node = new Uri("http://localhost:9200");
     var settings = new ConnectionSettings(node, defaultIndex: "logstash-2015.08.18");
     var client = new ElasticClient(settings);
     _client = client;
 }