示例#1
0
        private static void Execute(object sender, EventArgs e)
        {
            var logger = LogManager.GetTraceLogger("WEBJOB");

            logger.Trace(new TraceLogMessage(new { Command = "Execution Start" }, "Elasticsearch"));
            var sw = Stopwatch.StartNew();

            var indexedItemCount = 0;

            try
            {
                var itemDbCommand   = new ItemDbCommand(ConnectionString);
                var notIndexedItems = itemDbCommand.GetNotIndexedItemsAsync().Result;
                indexedItemCount = notIndexedItems.Count();

                var searchSearvice = new SearchService(ConnectionString);
                searchSearvice.BulkItemsAsync(notIndexedItems).Wait();
            }
            catch (Exception exception)
            {
                logger.Error(exception);
            }

            sw.Stop();
            logger.Trace(
                new TraceLogMessage(
                    new { itemDbCommand = "Execution End", IndexedItemCount = indexedItemCount },
                    "Elasticsearch",
                    sw.ElapsedMilliseconds));
        }
        public async Task RecreateEsIndexAsync(CancellationToken cancellationToken)
        {
            #region IndexParameters
            var indexparameters = new
            {
                index = new
                {
                    analysis = new
                    {
                        filter = new
                        {
                            kuromoji_rf = new
                            {
                                type       = "kuromoji_readingform",
                                use_romaji = true
                            },
                            kuromoji_pos = new
                            {
                                type = "kuromoji_part_of_speech",
                                enable_position_increment = false,
                                stoptags = new[] { "# verb-main:", "動詞-自立", "助詞-格助詞-一般", "助詞-終助詞" }
                            },
                            kuromoji_ks = new
                            {
                                type           = "kuromoji_stemmer",
                                minimum_length = 5
                            },
                            greek_lowercase_filter = new
                            {
                                type     = "lowercase",
                                language = "greek"
                            }
                        }
                    },
                    tokenizer = new
                    {
                        kuromoji = new
                        {
                            type = "kuromoji_tokenizer"
                        }
                    },
                    analyzer = new
                    {
                        kuromoji_analyzer = new
                        {
                            type      = "custom",
                            tokenizer = "kuromoji_tokenizer",
                            filter    =
                                new[]
                            {
                                "kuromoji_baseform", "kuromoji_ks", "kuromoji_pos", "greek_lowercase_filter",
                                "cjk_width"
                            }
                        }
                    }
                }
            };
            #endregion

            var node = new Uri(_config.ElasticsearchNode);
            var createContentJson = JsonConvert.SerializeObject(indexparameters);

            using (var createContent = new StringContent(createContentJson))
                using (var client = new HttpClient {
                    BaseAddress = node
                })
                {
                    await client.DeleteAsync(_config.ElasticsearchIndex, cancellationToken).ConfigureAwait(false);

                    using (
                        var res =
                            await
                            client.PutAsync(_config.ElasticsearchIndex, createContent, cancellationToken)
                            .ConfigureAwait(false))
                    {
                        res.EnsureSuccessStatusCode();
                    }
                }

            var itemDbCommand = new ItemDbCommand(_connectionString);
            var items         = await itemDbCommand.GetAllAsync(cancellationToken).ConfigureAwait(false);

            await BulkItemsAsync(items, cancellationToken).ConfigureAwait(false);
        }