예제 #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;
 }
예제 #2
0
        public PostIndexer()
        {
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(node);

            _client = new ElasticClient(settings);
        }
예제 #3
0
 public void TestFloatLookup()
 {
     var client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")));
     var expected = 12f;
     var id = new IdResolver().GetIdFor(new FloatIdClass { Id = expected });
     StringAssert.AreEqualIgnoringCase(expected.ToString(), id);
 }
        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);

        }
 public static void AddAutocompleteMappingToIndex(ElasticClient client)
 {
     //http://stackoverflow.com/questions/30285065/elasticsearch-nest-client-creating-multi-field-fields-with-completion
     client.CreateIndex(descriptor => descriptor
         .Index(EsIndexName)
         .AddMapping<Skill>(m => m
             .Properties(p => p.MultiField(mf => mf
                 .Name(n => n.Name)
                 .Fields(f => f
                     .String(s => s.Name(n => n.Name).Index(FieldIndexOption.Analyzed))
                     .String(s => s.Name(n => n.Name.Suffix("sortable")).Index(FieldIndexOption.NotAnalyzed))
                     .String(s => s.Name(n => n.Name.Suffix("autocomplete")).IndexAnalyzer("shingle_analyzer"))))))
         .AddMapping<Certification>(m => m
             .Properties(p => p.MultiField(mf => mf
                 .Name(n => n.Name)
                 .Fields(f => f
                     .String(s => s.Name(n => n.Name).Index(FieldIndexOption.Analyzed))
                     .String(s => s.Name(n => n.Name.Suffix("sortable")).Index(FieldIndexOption.NotAnalyzed))
                     .String(s => s.Name(n => n.Name.Suffix("autocomplete")).IndexAnalyzer("shingle_analyzer"))))))
         .Analysis(a => a
             .Analyzers(b => b.Add("shingle_analyzer", new CustomAnalyzer
             {
                 Tokenizer = "standard",
                 Filter = new List<string> {"lowercase", "shingle_filter"}
             }))
             .TokenFilters(b => b.Add("shingle_filter", new ShingleTokenFilter
             {
                 MinShingleSize = 2,
                 MaxShingleSize = 5
             })))
         );
 }
        //requires a data processor to ensure unique row ids
        public ElasticsearchStorage(IDataProcessor DataProcessor, string Server = null, string Index = null, string Username = null, string Password = null)
            : base(DataProcessor)
        {
            this.Server   = Framework.Settings.Get("ELASTICSEARCH_URL", Server);
            this.Index    = Framework.Settings.Get("ELASTICSEARCH_INDEX", Index);
            this.Username = Framework.Settings.Get("ELASTICSEARCH_USER", Username);
            this.Password = Framework.Settings.Get("ELASTICSEARCH_PWD", Password);

            if (String.IsNullOrEmpty(this.Server))
            {
                throw new Exception("No elasticsearch server defined!");
            }
            if (String.IsNullOrEmpty(this.Index))
            {
                throw new Exception("No elasticsearch index defined!");
            }

            _client = new Nest.ElasticClient(new ConnectionSettings(
                                                 new Uri(this.Server))
                                             .DefaultIndex(this.Index)
                                             .BasicAuthentication(this.Username, this.Password)
                                             .EnableHttpCompression()
                                             );

            this.Log("Elasticsearch URI: {0}", this.Server);
            this.Log("Elasticsearch Index: {0}", this.Index);
        }
 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);
 }
예제 #8
0
파일: AsyncTests.cs 프로젝트: Wigren/NEST
        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");
        }
예제 #9
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 void ShouldRoundtripDateTimeAndDateTimeOffsetWithSameKindAndOffset()
        {
            var dates = new Dates
            {
                DateTimeUtcKind = new DateTime(2016,1,1,1,1,1,DateTimeKind.Utc),
                DateTimeOffset = new DateTimeOffset(1999, 1, 1, 1, 1, 1, 1, TimeSpan.FromHours(5)),
                DateTimeOffsetUtc = new DateTimeOffset(1999, 1, 1, 1, 1, 1, 1, TimeSpan.Zero)
            };

            var client = new ElasticClient();
            var serializedDates = client.Serializer.SerializeToString(dates,SerializationFormatting.None);

            serializedDates.Should()
                .Be("{\"dateTimeUtcKind\":\"2016-01-01T01:01:01Z\"," +
                    "\"dateTimeOffset\":\"1999-01-01T01:01:01.001+05:00\"," +
                    "\"dateTimeOffsetUtc\":\"1999-01-01T01:01:01.001+00:00\"}");

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serializedDates)))
            {
                var deserializedDates = client.Serializer.Deserialize<Dates>(stream);

                deserializedDates.DateTimeUtcKind.Should().Be(dates.DateTimeUtcKind);
                deserializedDates.DateTimeUtcKind.Kind.Should().Be(dates.DateTimeUtcKind.Kind);

                deserializedDates.DateTimeOffset.Should().Be(dates.DateTimeOffset);
                deserializedDates.DateTimeOffset.Offset.Should().Be(dates.DateTimeOffset.Offset);
                deserializedDates.DateTimeOffset.Date.Kind.Should().Be(dates.DateTimeOffset.Date.Kind);

				deserializedDates.DateTimeOffsetUtc.Should().Be(dates.DateTimeOffsetUtc);
                deserializedDates.DateTimeOffsetUtc.Offset.Should().Be(dates.DateTimeOffsetUtc.Offset);
                deserializedDates.DateTimeOffsetUtc.Date.Kind.Should().Be(dates.DateTimeOffsetUtc.Date.Kind);
            }
        }
예제 #11
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");
            }
        }
예제 #12
0
		public ProductRepository(IAggregateFactory aggregateFactory)
		{
			_client = new ElasticClient();
			_factory = aggregateFactory;

			Mapper.CreateMap<Product, ProductDTO>();
		}
예제 #13
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;
     }
 }
예제 #14
0
        public Nest.ElasticClient GetClient()
        {
            var connectionSettings = this.CreateDefaulConnectionSettings();
            var client             = new Nest.ElasticClient(connectionSettings);

            return(client);
        }
예제 #15
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;
     }
 }
        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;
        }
예제 #17
0
 public void TestDoubleLookup()
 {
     var client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")));
     var expected = 12d;
     var id = client.GetIdFor(new DoubleIdClass { Id = expected });
     StringAssert.AreEqualIgnoringCase(expected.ToString(), id);
 }
 private static ElasticClient ElasticClientForIndexingFiles(
     IConnectionSettingsValues connectionSettingsForIndexingFiles)
 {
     var elasticClientForIndexingFiles =
         new ElasticClient(connectionSettingsForIndexingFiles);
     return elasticClientForIndexingFiles;
 }
예제 #19
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);
        }
		/**== 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);
		}
예제 #21
0
        public async static Task <IDeleteIndexResponse> DeleteIndexAsync(string elasticSearchConnectionString, string index)
        {
            var req = new DeleteIndexRequest(index);

            var client = new Nest.ElasticClient(new Uri(elasticSearchConnectionString));

            return(await client.DeleteIndexAsync(req));
        }
예제 #22
0
        public static ValidateQueryResponse ValidateSpecificQueryRaw <T>(Nest.ElasticClient client, QueryContainer qc)
            where T : class
        {
            var res = client.Indices
                      .ValidateQuery <T>(v => v
                                         .Query(q => qc)
                                         );

            return(res);
        }
        public static void LoadData()
        {
            var nodes = new List <Uri> {
                new Uri("http://*****:*****@"C:\cities_canada-usa.csv"))
            {
                if (!reader.EndOfStream)
                {
                    reader.ReadLine();
                }

                var documents = new List <City>();
                while (!reader.EndOfStream)
                {
                    var line   = reader.ReadLine();
                    var values = line.Split(';');

                    //0 - id

                    //1 - name

                    //3 - alt_name

                    //4 - lat

                    //5 - lon

                    //8 - country_code
                    documents.Add(new City
                    {
                        Id       = int.Parse(values[0]),
                        Name     = values[1],
                        AltName  = values[3],
                        Country  = values[8],
                        Location = new Location(double.Parse(values[4]), double.Parse(values[5])),
                        FullText = string.Concat(values[1], ", ", values[8])
                    });
                }

                var result = client.IndexMany(documents, "city");
            }
        }
예제 #24
0
        public static void CreateIndex(string elasticSearchConnectionString, string index, int shards, int replicas)
        {
            CreateIndexRequest req = new CreateIndexRequest(index);

            req.Settings = new IndexSettings
            {
                NumberOfReplicas = replicas,
                NumberOfShards   = shards
            };

            var client = new Nest.ElasticClient(new Uri(elasticSearchConnectionString));
            var res    = client.CreateIndex(req);
        }
예제 #25
0
        // GET: Home
        public ActionResult Index()
        {
            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(connectionPool)
                           .DefaultIndex("product")
                           .DisableDirectStreaming()
                           .OnRequestCompleted(RequestHandler.OnRequestCompletedHandler);
            var client = new Nest.ElasticClient(settings);

            ISearchResponse <EsDocument> data = client.Search <EsDocument>(s => s.Query(q => q.MatchAll()));

            return(View(data.Documents));
        }
예제 #26
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 DataContracts.IndexResponse AddDocument(IndexRequest query)
        {
            ESI4TLogger.WriteLog(ELogLevel.INFO,
                                 "Entering ESI4TIndexManager.AddDocument for TCM URI: " +
                                 query.ItemURI);

            DataContracts.IndexResponse response = new DataContracts.IndexResponse();

            OperationResult result = OperationResult.Failure;

            try
            {
                XmlDocument doc = new XmlDocument();
                string      ID  = string.Empty;
                doc.LoadXml(Utility.UpdateContentTypeXML(Regex.Replace(query.DCP.ToString(), @"\b'\b", "")));
                string jsonText = JsonConvert.SerializeXmlNode(doc);

                //     var bln = Deserialize<Esnews>(doc);

                var conString = ESI4TServicesConstants.elasticSearch_URL;
                ESI4TLogger.WriteLog(ELogLevel.INFO, "conString: " + conString);

                node     = new Uri(conString);
                settings = new ConnectionSettings(node);
                settings.DefaultIndex("fromelasticstoweb8");
                var client        = new Nest.ElasticClient(settings);
                var indexResponse = client.LowLevel.Index <string>("fromelasticstoweb8", "esnews", jsonText);
                //     var responseBool = client.Index(bln);
                result = OperationResult.Success;
            }
            catch (Exception ex)
            {
                string logString = ESI4TServiceConstants.LOG_MESSAGE + Environment.NewLine;

                logString = string.Concat(logString,
                                          Environment.NewLine,
                                          string.Format("{0}{1}", ex.Message, ex.StackTrace));

                ESI4TLogger.WriteLog(ELogLevel.ERROR, logString);
                result = OperationResult.Failure;
            }
            response.Result = (int)result;
            ESI4TLogger.WriteLog(ELogLevel.INFO,
                                 "Exiting ESI4TIndexManager.AddDocument, Result: " +
                                 result.ToString());

            return(response);
        }
예제 #28
0
        protected DataSet(string datasourceName, bool fireException)
        {
            this.datasetId = datasourceName.ToLower();
            this.client    = Lib.ES.Manager.GetESClient(datasetId, idxType: ES.Manager.IndexType.DataSource);


            var ret = client.Indices.Exists(client.ConnectionSettings.DefaultIndex); //todo: es7 check

            if (ret.Exists == false)
            {
                if (fireException)
                {
                    throw new DataSetException(this.datasetId, ApiResponseStatus.DatasetNotFound);
                }
                else
                {
                    this.client = null;
                }
            }
        }
예제 #29
0
파일: RefreshTree.cs 프로젝트: netojoa/blog
        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);
        }
예제 #30
0
 public Mouth(Contexts.Mouths.Mouth destination)
 {
     _destination = destination;
     _client      = new Nest.ElasticClient(new Nest.ConnectionSettings(new Uri(destination.Url)));
 }
예제 #31
0
 protected NamespacedClientProxy(ElasticClient client) => _client = client;
예제 #32
0
        static void Main(string[] args)
        {
            var dataFolder   = @"G:\src\hackatons\chaintrack\storage\decisions\";
            var crawlerIndex = @"G:\src\hackatons\chaintrack\storage\decisions\data.json";
            var settings     = new ConnectionSettings(new Uri("http://localhost:9200"))
                               .DefaultIndex("documents").DisableDirectStreaming(true);
            var crawled = JsonConvert.DeserializeObject <CrawlersResult>(File.ReadAllText(crawlerIndex));


            var client = new Nest.ElasticClient(settings);
            var createIndexResponse = client.Indices.Create("documents", c => c
                                                            .Settings(s => s

                                                                      .Analysis(a => a

                                                                                .TokenFilters(f => f
                                                                                              .Stemmer("russian_stemmer", st => st.Language("russian"))
                                                                                              .Stop("russian_stop", st => st.StopWords("_russian_"))
                                                                                              )
                                                                                .Analyzers(aa => aa
                                                                                           .Custom("ru",
                                                                                                   ca => ca
                                                                                                   .Tokenizer("standard")
                                                                                                   .Filters("russian_stop", "russian_stemmer")
                                                                                                   // TODO: lowercase
                                                                                                   )
                                                                                           )
                                                                                )

                                                                      )
                                                            .Map(x => x.AutoMap().Properties(pz =>
                                                                                             pz.Text(tttt => tttt.Name("heading").Analyzer("ru"))
                                                                                             .Text(tttt => tttt.Name("group").Analyzer("ru"))
                                                                                             .Text(tttt => tttt.Name("name").Analyzer("ru"))
                                                                                             .Text(tttt => tttt.Name("body").Analyzer("ru"))
                                                                                             .Text(tttt => tttt.Name("institute").Analyzer("ru"))
                                                                                             .Text(tttt => tttt.Name("reactionSlug").Analyzer("ru"))
                                                                                             )
                                                                 )

                                                            );

            Console.WriteLine(createIndexResponse.DebugInformation);
            foreach (var c in crawled.Entries)
            {
                Console.WriteLine(c.name);

                var entry = new LawEntry();
                entry.group        = c.group;
                entry.heading      = c.heading;
                entry.name         = c.name;
                entry.number       = c.number;
                entry.body         = File.ReadAllText(Path.Combine(dataFolder, c.textFile));
                entry.institute    = c.institute;
                entry.publishedAt  = DateTime.ParseExact(c.date, "dd.MM.yyyy", CultureInfo.CreateSpecificCulture("ru"));
                entry.reactionSlug = c.reactionSlug != null && c.reactionSlug.Any()? c.reactionSlug.Aggregate((acc, e) => acc + ", " + e) : null;
                Console.WriteLine("21312213 " + entry.reactionSlug);
                client.IndexDocument(entry);
            }

            Console.WriteLine("Hello World!");
        }
예제 #33
0
 static ElasticClient()
 {
     DeserializeSettings   = ElasticClient.CreateDeserializeSettings();
     SerializationSettings = ElasticClient.CreateSettings();
     PropertyNameResolver  = new PropertyNameResolver(SerializationSettings);
 }
예제 #34
0
 public BuildRepository(IClientBuilder clientBuilder)
 {
     this.client = clientBuilder.GetClient();
 }
예제 #35
0
        public async static Task <ICatResponse <CatIndicesRecord> > ListIndexAsync(string elasticSearchConnectionString)
        {
            var client = new Nest.ElasticClient(new Uri(elasticSearchConnectionString));

            return(await client.CatIndicesAsync());
        }
예제 #36
0
        public static ISearchResponse <object> _searchData(DataSet ds, string queryString, int page, int pageSize, string sort = null,
                                                           bool excludeBigProperties = true, bool withHighlighting = false, bool exactNumOfResults = false)
        {
            SortDescriptor <object> sortD = new SortDescriptor <object>();

            if (sort == "0")
            {
                sort = null;
            }

            if (!string.IsNullOrEmpty(sort))
            {
                if (sort.EndsWith(DataSearchResult.OrderDesc) || sort.ToLower().EndsWith(DataSearchResult.OrderDescUrl))
                {
                    sort  = sort.Replace(DataSearchResult.OrderDesc, "").Replace(DataSearchResult.OrderDescUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Descending);
                }
                else
                {
                    sort  = sort.Replace(DataSearchResult.OrderAsc, "").Replace(DataSearchResult.OrderAscUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Ascending);
                }
            }


            Nest.ElasticClient client = Lib.ES.Manager.GetESClient(ds.DatasetId, idxType: ES.Manager.IndexType.DataSource);

            QueryContainer qc = GetSimpleQuery(ds, queryString);

            //QueryContainer qc = null;
            //if (queryString == null)
            //    qc = new QueryContainerDescriptor<object>().MatchNone();
            //else if (string.IsNullOrEmpty(queryString))
            //    qc = new QueryContainerDescriptor<object>().MatchAll();
            //else
            //{
            //    qc = new QueryContainerDescriptor<object>()
            //        .QueryString(qs => qs
            //            .Query(queryString)
            //            .DefaultOperator(Operator.And)
            //        );
            //}

            page = page - 1;
            if (page < 0)
            {
                page = 0;
            }
            if (page * pageSize > Lib.Data.Smlouva.Search.MaxResultWindow)
            {
                page = (Lib.Data.Smlouva.Search.MaxResultWindow / pageSize) - 1;
            }

            //exclude big properties from result
            var maps = excludeBigProperties ? ds.GetMappingList("DocumentPlainText").ToArray() : new string[] { };



            var res = client
                      .Search <object>(s => s
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       .Highlight(h => Lib.Searching.Tools.GetHighlight <Object>(withHighlighting))
                                       .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                       );

            //fix Highlighting for large texts
            if (withHighlighting &&
                res.Shards != null &&
                res.Shards.Failed > 0)    //if some error, do it again without highlighting
            {
                res = client
                      .Search <object>(s => s
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       .Highlight(h => Lib.Searching.Tools.GetHighlight <Object>(false))
                                       .TrackTotalHits(exactNumOfResults ? true : (bool?)null)
                                       );
            }

            Audit.Add(Audit.Operations.Search, "", "", "Dataset." + ds.DatasetId, res.IsValid ? "valid" : "invalid", queryString, null);

            return(res);
        }
예제 #37
0
 public TopChildrenQueryDescriptor()
 {
     this._Type = ElasticClient.GetTypeNameFor <T>();
 }
예제 #38
0
 public ElasticClient(Nest.ElasticClient cli) => _cli = cli;
예제 #39
0
 public HasChildFilterDescriptor()
 {
     this._Type = ElasticClient.GetTypeNameFor <T>();
 }
예제 #40
0
        public static ISearchResponse <object> _searchData(DataSet ds, string queryString, int page, int pageSize, string sort = null, bool excludeBigProperties = true)
        {
            SortDescriptor <object> sortD = new SortDescriptor <object>();

            if (sort == "0")
            {
                sort = null;
            }

            if (!string.IsNullOrEmpty(sort))
            {
                if (sort.EndsWith(DataSearchResult.OrderDesc) || sort.ToLower().EndsWith(DataSearchResult.OrderDescUrl))
                {
                    sort  = sort.Replace(DataSearchResult.OrderDesc, "").Replace(DataSearchResult.OrderDescUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Descending);
                }
                else
                {
                    sort  = sort.Replace(DataSearchResult.OrderAsc, "").Replace(DataSearchResult.OrderAscUrl, "").Trim();
                    sortD = sortD.Field(sort, SortOrder.Ascending);
                }
            }


            Nest.ElasticClient client = Lib.ES.Manager.GetESClient(ds.DatasetId, idxType: ES.Manager.IndexType.DataSource);

            QueryContainer qc = GetSimpleQuery(ds, queryString);

            //QueryContainer qc = null;
            //if (queryString == null)
            //    qc = new QueryContainerDescriptor<object>().MatchNone();
            //else if (string.IsNullOrEmpty(queryString))
            //    qc = new QueryContainerDescriptor<object>().MatchAll();
            //else
            //{
            //    qc = new QueryContainerDescriptor<object>()
            //        .QueryString(qs => qs
            //            .Query(queryString)
            //            .DefaultOperator(Operator.And)
            //        );
            //}

            page = page - 1;
            if (page < 0)
            {
                page = 0;
            }
            if (page * pageSize > Lib.ES.SearchTools.MaxResultWindow)
            {
                page = (Lib.ES.SearchTools.MaxResultWindow / pageSize) - 1;
            }

            //exclude big properties from result
            var maps = excludeBigProperties ? ds.GetMappingList("DocumentPlainText").ToArray() : new string[] { };


            var res = client
                      .Search <object>(s => s
                                       .Type("data")
                                       .Size(pageSize)
                                       .Source(ss => ss.Excludes(ex => ex.Fields(maps)))
                                       .From(page * pageSize)
                                       .Query(q => qc)
                                       .Sort(ss => sortD)
                                       );

            return(res);
        }
예제 #41
0
 public static bool ValidateQuery <T>(Nest.ElasticClient client, QueryContainer qc)
     where T : class
 {
     return(ValidateSpecificQueryRaw <T>(client, qc)?.Valid ?? false);
 }
        /// <summary>
        /// This method removes an index from Elastic
        /// </summary>
        /// <param name="query">IndexRequest containing delete criteria</param>
        /// <returns>IndexResponse indicating success or failure</returns>
        public DataContracts.IndexResponse RemoveDocument(IndexRequest query)
        {
            ESI4TLogger.WriteLog(ELogLevel.INFO, "Entering ESI4TIndexManager.RemoveDocument for TCM URI: " +
                                 query.ItemURI);
            DataContracts.IndexResponse response   = new DataContracts.IndexResponse();
            JavaScriptSerializer        serializer = new JavaScriptSerializer();

            serializer.MaxJsonLength = Int32.MaxValue;
            var             webClient = new WebClient();
            OperationResult result    = OperationResult.Failure;

            try
            {
                XmlDocument doc     = new XmlDocument();
                string      ID      = query.ItemURI;
                string      strId   = "\"" + ID + "\"";
                var         content = webClient.DownloadString(@"http://*****:*****@"\b'\b", "")));
                foreach (var item in data)
                {
                    var aa = item;
                    if (aa.Key == "hits")
                    {
                        foreach (var item2 in aa.Value)
                        {
                            var aaaa = item2;
                            if (aaaa.Key == "hits")
                            {
                                foreach (var item3 in aaaa.Value)
                                {
                                    foreach (var item4 in item3)
                                    {
                                        if (item4.Key == "_id")
                                        {
                                            Id      = item4.Key;
                                            idValue = item4.Value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //var bln = Deserialize<Esnews>(doc);
                node = new Uri("http://localhost:9200");

                settings = new ConnectionSettings(node);

                settings.DefaultIndex("fromelasticstoweb8");
                var client         = new Nest.ElasticClient(settings);
                var responseReturn = client.Delete <Esnews>(idValue, d => d
                                                            .Index("fromelasticstoweb8")
                                                            .Type("esnews"));
                result = OperationResult.Success;
                ESI4TLogger.WriteLog(ELogLevel.INFO, "Exit ESI4TIndexManager.RemoveDocument for TCM URI: " +
                                     query.ItemURI + " result " + result);
            }
            catch (Exception ex)
            {
                string logString = ESI4TServiceConstants.LOG_MESSAGE + Environment.NewLine;
                logString = string.Concat(logString,
                                          string.Format("Item URI : {0}", query.ItemURI),
                                          Environment.NewLine, string.Format("{0}{1}", ex.Message, ex.StackTrace));
                ESI4TLogger.WriteLog(ELogLevel.ERROR, logString);
                result = OperationResult.Failure;
            }

            response.Result = (int)result;

            ESI4TLogger.WriteLog(ELogLevel.INFO,
                                 "Exiting ESI4TIndexManager.RemoveDocument, Result: " +
                                 result.ToString());

            return(response);
        }
        // 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);
        }
        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");
        }
예제 #45
-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);
                    }
                }
            }
        }
예제 #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();
        }
예제 #47
-1
 public void construct_client_with_null()
 {
     Assert.Throws<ArgumentNullException>(() =>
     {
         var client = new ElasticClient(null);
     });
 }
예제 #48
-1
		[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");

		}
예제 #49
-1
 public void TestAlternateIdLookup()
 {
     var client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")));
     var expectedGuid = Guid.NewGuid();
     var id = new IdResolver().GetIdFor(new AlternateIdClass { Guid = expectedGuid });
     StringAssert.AreEqualIgnoringCase(expectedGuid.ToString(), id);
 }
예제 #50
-1
 private SearchAdapter()
 {
     esconnSetting = new ConnectionSettings(
                         new Uri(ESConnectionSetting.Node), "lt028310");
     esClient = new ElasticClient(esconnSetting);
     
 }
예제 #51
-1
 protected static void GenerateAndIndex(ElasticClient client, string indexName, int numMessages, int bufferSize)
 {
     var msgGenerator = new MessageGenerator();
     var tasks = new List<Task>();
     var partitionedMessages = msgGenerator.Generate(numMessages).Partition(bufferSize);
     client.CreateIndex(indexName, c => c
         .NumberOfReplicas(0)
         .NumberOfShards(1)
         .Settings(s => s.Add("refresh_interval", "-1"))
         .AddMapping<Message>(p=>p.MapFromAttributes())
     );
     Interlocked.Exchange(ref NumSent, 0);
     foreach (var messages in partitionedMessages)
     {
         var t = client.IndexManyAsync(messages, indexName)
             .ContinueWith(tt =>
             {
                 Interlocked.Add(ref NumSent, bufferSize);
                 Console.WriteLine("Sent {0:0,0} messages to {1}, {2}", NumSent, indexName, tt.Result.Took);
             })
             ;
         tasks.Add(t);
     }
     Task.WaitAll(tasks.ToArray());
     client.UpdateSettings(u => u
         .Index(indexName)
         .RefreshInterval("1s")
     );
 }
예제 #52
-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;
 }
		/** = Connection Pooling
		 * Connection pooling is the internal mechanism that takes care of registering what nodes there are in the cluster and which
		 * we can use to issue client calls on.
		 */
		
		/** == SingleNodeConnectionPool 
		* The simplest of all connection pools, this takes a single `Uri` and uses that to connect to elasticsearch for all the calls
		* It doesn't opt in to sniffing and pinging behavior, and will never mark nodes dead or alive. The one `Uri` it holds is always
		* ready to go. 
		*/
		[U] public void SingleNode()
		{
			var uri = new Uri("http://localhost:9201");
			var pool = new SingleNodeConnectionPool(uri);
			pool.Nodes.Should().HaveCount(1);
			var node = pool.Nodes.First();
			node.Uri.Port.Should().Be(9201);

			/** This type of pool is hardwired to optout out sniffing*/
			pool.SupportsReseeding.Should().BeFalse();
			/** and pinging */
			pool.SupportsPinging.Should().BeFalse();

			/** When you use the low ceremony ElasticClient constructor that takes a single Uri
			* We default to this SingleNodeConnectionPool */
			var client = new ElasticClient(uri);
			client.ConnectionSettings.ConnectionPool.Should().BeOfType<SingleNodeConnectionPool>();

			/** However we urge that you always pass your connection settings explicitly */
			client = new ElasticClient(new ConnectionSettings(uri));
			client.ConnectionSettings.ConnectionPool.Should().BeOfType<SingleNodeConnectionPool>();

			/** or even better pass the connection pool explicitly  */
			client = new ElasticClient(new ConnectionSettings(pool));
			client.ConnectionSettings.ConnectionPool.Should().BeOfType<SingleNodeConnectionPool>();
		}
예제 #54
-1
파일: Program.cs 프로젝트: jptoto/esdotnet
        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();
        }