Esempio n. 1
0
        public void TestCreatingMapping()
        {
            var index = "index_operate" + Guid.NewGuid().ToString();
            StringFieldSetting stringFieldSetting = new StringFieldSetting()
            {
                Analyzer = "standard", Type = "string", NullValue = "mystr"
            };

            TypeSetting typeSetting = new TypeSetting("custom_type");

            typeSetting.AddFieldSetting("medcl", stringFieldSetting);

            var typeSetting2 = new TypeSetting("hell_type1");
            var numfield     = new NumberFieldSetting()
            {
                Store = Store.yes, NullValue = 0.00
            };

            typeSetting2.AddFieldSetting("name", numfield);

            client.CreateIndex(index);
            var result = client.PutMapping(index, typeSetting);

            Assert.AreEqual(true, result.Success);

            result = client.PutMapping(index, typeSetting2);
            Assert.AreEqual(true, result.Success);

            var result2 = client.DeleteIndex(index);

            Assert.AreEqual(true, result2.Success);
        }
Esempio n. 2
0
        public void TestTemplate()
        {
            var tempkey = "test_template_key1";

            client.DeleteTemplate(tempkey);

            var template = new TemplateSetting(tempkey);

            template.Template     = "business_*";
            template.IndexSetting = new TemplateIndexSetting(3, 2);

            var type1 = new TypeSetting("mytype")
            {
            };

            type1.AddNumField("identity", NumType.Float);
            type1.AddDateField("datetime");

            var type2 = new TypeSetting("mypersontype");

            type2.AddStringField("personid");

            type2.SourceSetting         = new SourceSetting();
            type2.SourceSetting.Enabled = false;

            template.AddTypeSetting(type1);
            template.AddTypeSetting(type2);

            var jsonstr = JsonSerializer.Get(template);

            Console.WriteLine(jsonstr);

            var result = client.CreateTemplate(tempkey, template);

            Console.WriteLine(result.JsonString);
            Assert.AreEqual(true, result.Success);

            result = client.CreateIndex("business_111");
            Assert.AreEqual(true, result.Success);
            result = client.CreateIndex("business_132");
            Assert.AreEqual(true, result.Success);
            result = client.CreateIndex("business_31003");
            Assert.AreEqual(true, result.Success);

            client.Refresh();
            var temp = client.GetTemplate(tempkey);

            TemplateSetting result1;

            Assert.AreEqual(true, temp.TryGetValue(tempkey, out result1));
            Assert.AreEqual(template.Order, result1.Order);
            Assert.AreEqual(template.Template, result1.Template);

            client.DeleteIndex("business_111");
            client.DeleteIndex("business_132");
            client.DeleteIndex("business_31003");
        }
Esempio n. 3
0
        public void TestCreateParentChildType()
        {
            var index = "index_test_parent_child_type";



            var parentType = new TypeSetting("blog");

            parentType.AddStringField("title");

            var client = new ElasticSearchClient("localhost");

            client.CreateIndex(index);

            var op = client.PutMapping(index, parentType);

            Assert.AreEqual(true, op.Acknowledged);

            var childType = new TypeSetting("comment", parentType);

            childType.AddStringField("comments");

            op = client.PutMapping(index, childType);
            Assert.AreEqual(true, op.Acknowledged);

            var mapping = client.GetMapping(index, "comment");

            Assert.True(mapping.IndexOf("_parent") > 0);

            client.DeleteIndex(index);
        }
Esempio n. 4
0
        public void TestBulkForFramdThrift()
        {
            var client = new ElasticSearchClient("localhost");

            var fields = new Dictionary <string, object>();

            fields.Add("name", "jack");
            fields.Add("age", 25);
            var index = "index_bulk_framed";

            try
            {
                client.DeleteIndex(index);
                client.CreateIndex(index);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            var jsondata = JsonSerializer.Get(fields);

            var result = client.Bulk(new List <BulkObject>()
            {
                new BulkObject()
                {
                    Id = "1", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "2", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "3", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "4", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "5", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "6", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "7", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "8", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
            });

            Assert.AreEqual(true, result.Success);
        }
Esempio n. 5
0
 private void newIndexToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (newIndex.ShowDialog() == DialogResult.OK)
     {
         WriteLog("Create New Index: {0},{1},{2}", newIndex.IndexName, newIndex.Shard, newIndex.Replica);
         var result = currentElasticSearchInstance.CreateIndex(newIndex.IndexName,
                                                               new IndexSetting(newIndex.Shard, newIndex.Replica));
     }
 }
        private static void ImportNocPostitle(ConnectionString connectionString, string fileName)
        {
            var client = new ElasticSearchClient <NocPostitle>(connectionString, "indexb");

            // Creates the Index, if neccessary:
            client.CreateIndex();

            // Bulk Insert Data:
            foreach (var batch in GetNocPostitle(fileName).Batch(100))
            {
                var response = client.BulkInsert(batch);
            }
        }
Esempio n. 7
0
        public void TestBulkIndexWithParentId()
        {
            var client = new ElasticSearchClient("localhost");

            var fields = new Dictionary <string, object>();

            fields.Add("name", "jack");
            fields.Add("age", 25);
            var index = "index_12312312311";

            try
            {
                client.DeleteIndex(index);
                client.CreateIndex(index);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            var jsondata = JsonSerializer.Get(fields);

            var result = client.Bulk(new List <BulkObject>()
            {
                new BulkObject()
                {
                    Id = "1", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "2", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "3", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                }
            });

            Assert.AreEqual(true, result.Success);
            client.Refresh();
            var c = client.Count(index, "type", "age:25");

            Assert.AreEqual(3, c);
            result = client.Delete(index, "type", new string[] { "1", "2", "3" }, "1");
            Assert.AreEqual(true, result.Success);
            client.Refresh();
            c = client.Count(index, "type", "age:25");
            Assert.AreEqual(0, c);
            client.DeleteIndex(index);
        }
        public static void Main(string[] args)
        {
            var connectionString = new ConnectionString("http", "localhost", 9200);

            // Create a new Client, that writes the Weater Data and creates the Index weather_data:
            var client = new ElasticSearchClient <ElasticLocalWeatherDataType>(connectionString, "weather_data");

            // Creates the Index, if neccessary:
            client.CreateIndex();

            // Bulk Insert Data:
            foreach (var batch in GetData().Batch(100))
            {
                var response = client.BulkInsert(batch);
            }
        }
        /// <summary>
        ///
        /// https://www.elastic.co/guide/en/elasticsearch/reference/master/tune-for-indexing-speed.html
        /// </summary>
        /// <param name="csvFilePath"></param>
        private static void ProcessLocalWeatherData(string csvFilePath)
        {
            if (log.IsInfoEnabled)
            {
                log.Info($"Processing File: {csvFilePath}");
            }

            // Construct the Batch Processor:
            var client = new ElasticSearchClient <Elastic.Model.LocalWeatherData>(ConnectionString, "weather_data");

            // We are creating the Index with special indexing options for initial load,
            // as suggested in the Elasticsearch documentation at [1].
            //
            // We disable the performance-heavy indexing during the initial load and also
            // disable any replicas of the data. This comes at a price of not being able
            // to query the data in realtime, but it will enhance the import speed.
            //
            // After the initial load I will revert to the standard settings for the Index
            // and set the default values for Shards and Refresh Interval.
            //
            // [1]: https://www.elastic.co/guide/en/elasticsearch/reference/master/tune-for-indexing-speed.html
            //
            client.CreateIndex(settings => settings
                               .NumberOfReplicas(0)
                               .RefreshInterval(-1));

            // Access to the List of Parsers:
            var batches = Parsers
                          // Use the LocalWeatherData Parser:
                          .LocalWeatherDataParser
                          // Read the File, Skip first row:
                          .ReadFromFile(csvFilePath, Encoding.UTF8, 1)
                          // Get the Valid Results:
                          .Where(x => x.IsValid)
                          // And get the populated Entities:
                          .Select(x => x.Result)
                          // Convert to ElasticSearch Entity:
                          .Select(x => LocalWeatherDataConverter.Convert(x))
                          // Batch Entities:
                          .Batch(30000);


            foreach (var batch in batches)
            {
                client.BulkInsert(batch);
            }
        }
Esempio n. 10
0
        public static void Main(string[] args)
        {
            var uri = new Uri("http://localhost:9200");

            // Create a new Client, that writes the Weater Data and creates the Index weather_data:
            var client = new ElasticSearchClient <ElasticLocalWeatherDataType>(uri, "weather_data");

            // Creates the Index, if neccessary:
            client.CreateIndex();

            // Bulk Insert Data:
            foreach (var batch in GetData().Batch(100))
            {
                var response = client.BulkInsert(batch);

                if (response.Errors)
                {
                    Console.Error.WriteLine($"Bulk Write failed. Reason: {response.OriginalException.Message}");
                }
            }
        }
Esempio n. 11
0
        private void EnsureIndexInElasticCreated()
        {
            var response = _client.IndexExists(_options.RequestsIndexName);

            if (response.Exists)
            {
                return;
            }

            _client.CreateIndex(_options.RequestsIndexName, index => index
                                .Mappings(ms => ms
                                          .Map <IndexRequestDbo>(m => m
                                                                 .Properties(ps => ps
                                                                             .Keyword(k => k.Name(x => x.Url))
                                                                             .Date(d => d.Name(x => x.CreatedTime))
                                                                             .Text(t => t.Name(x => x.ErrorMessage))
                                                                             )
                                                                 )
                                          )
                                );
        }
Esempio n. 12
0
        private void EnsureIndexInElasticCreated()
        {
            var response = _client.IndexExists(_options.DocumentsIndexName);

            if (response.Exists)
            {
                return;
            }

            _client.CreateIndex(_options.DocumentsIndexName, index => index
                                .Settings(ElasticSearchOptions.AnalysisSettings)
                                .Mappings(mappings => mappings
                                          .Map <Document>(map => map
                                                          .Properties(properties => properties
                                                                      .Text(ElasticSearchOptions.TitleProperty)
                                                                      .Text(ElasticSearchOptions.TextProperty)
                                                                      .Keyword(ElasticSearchOptions.UrlProperty)
                                                                      ).SourceField(source => source
                                                                                    .Excludes(new[] { "text" })
                                                                                    )
                                                          )
                                          )
                                );
        }
Esempio n. 13
0
        public void TestHighlight()
        {
            ElasticSearch.Client.ElasticSearchClient client = new ElasticSearchClient("localhost", 9200, TransportType.Http);


            string indexName = Guid.NewGuid().ToString();


            client.CreateIndex(indexName);


            TypeSetting type = new TypeSetting("type");

            type.AddStringField("title").Analyzer   = "whitespace";
            type.AddStringField("snippet").Analyzer = "whitespace";
            client.PutMapping(indexName, type);

            //index sample
            Dictionary <string, object> dict = new Dictionary <string, object>();

            dict["title"]   = "quick fox jump away";
            dict["snippet"] = "quick fox jump away,where are you?";
            client.Index(indexName, "type", "1", dict);

            dict            = new Dictionary <string, object>();
            dict["title"]   = "fox river is nearby";
            dict["snippet"] = "where is fox river,where is it?";
            client.Index(indexName, "type", "2", dict);


            ElasticQuery query = new ElasticQuery(
                new QueryStringQuery("fox")
                .AddField("title", 5)
                .AddField("snippet", 5), null, 0, 5);

            query.AddHighlightField(new HightlightField("title"));
            query.AddHighlightField(new HightlightField("snippet"));

            client.Refresh(indexName);

            var result = client.Search(indexName, query);

            Console.Out.WriteLine(result.Query);
            Console.Out.WriteLine(result.Response);

            Console.Out.WriteLine("---");
            HitStatus hits = result.GetHits();

            if (hits != null)
            {
                foreach (var o in hits.Hits)
                {
                    foreach (var pair in o.Highlight)
                    {
                        Console.Out.WriteLine(pair.Key + ":");
                        foreach (var field in pair.Value)
                        {
                            Console.Out.WriteLine(field);
                        }

                        Console.Out.WriteLine();
                    }
                }
            }


            client.DeleteIndex(indexName);
        }
Esempio n. 14
0
 public void TestTypeMissingException()
 {
     client.CreateIndex("notypIndex");
     client.Search("notypIndex", "typeA", "match_all");
 }