private void EnsureIndexExists(ElasticClient esClient, string indexName)
        {
            var indexExistsRequest = new IndexExistsRequest(indexName);
            var indexExistsResult = esClient.IndexExists(indexExistsRequest);

            if (indexExistsResult.Exists == false)
            {
                var createIndexRequest = new CreateIndexRequest(indexName);

                var createIndexResponse = esClient.CreateIndex(createIndexRequest);

                if (createIndexResponse.Acknowledged == false)
                {
                    throw new ApplicationException(string.Format("Kunne ikke lave {0} index", indexName));
                }
            }
        }
        public ActionResult ReIndexAll()
        {
            var documents = db.DocumentModels.ToList();

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

            var settings = new ConnectionSettings(searchBoxUri);
            settings.SetDefaultIndex(indexName);

            var client = new ElasticClient(settings);

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

            // Create a new "sample" index with default settings
            //client.CreateIndex("sample", new IndexSettings());
            ICreateIndexRequest iCreateIndexReq = new CreateIndexRequest(indexName);
            iCreateIndexReq.IndexSettings = new IndexSettings();
            iCreateIndexReq.IndexSettings.NumberOfReplicas = 10;

            //client.CreateIndex(iCreateIndexReq);

            var resCreate = client.CreateIndex(indexName, s => s.AddMapping<DocumentModel>(f => f.MapFromAttributes()).NumberOfReplicas(1).NumberOfShards(10));

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

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

            return RedirectToAction("Index");
        }
 public CreateIndexRequest(IndexName index, IIndexState state) : this(index)
 {
     this.Settings = state.Settings;
     this.Mappings = state.Mappings;
     CreateIndexRequest.RemoveReadOnlySettings(this.Settings);
 }
        private static void Index(DocumentModel document, String operation)
        {
            var uriString = ConfigurationManager.AppSettings["SEARCHBOX_URL"];
            var searchBoxUri = new Uri(uriString);

            var settings = new ConnectionSettings(searchBoxUri);
            settings.SetDefaultIndex(indexName);

            var client = new ElasticClient(settings);

            if (!client.IndexExists(indexName).Exists)
            {
                // Create a new "sample" index with default settings
                ICreateIndexRequest iCreateIndexReq = new CreateIndexRequest(indexName);
                iCreateIndexReq.IndexSettings = new IndexSettings();
                iCreateIndexReq.IndexSettings.NumberOfReplicas = 10;

                //iCreateIndexReq.IndexSettings.Mappings = new List<RootObjectMapping>();
                //RootObjectMapping rootObjectMapping = new RootObjectMapping();
                //rootObjectMapping.AllFieldMapping()
                //iCreateIndexReq.IndexSettings.Mappings.
                //client.CreateIndex(iCreateIndexReq);
                //client.CreateIndex(indexName,s=>s.)
                var resCreate = client.CreateIndex(indexName, s => s.AddMapping<DocumentModel>(f => f.MapFromAttributes()).NumberOfReplicas(1).NumberOfShards(10));

                //client.CreateIndex(indexName, new IndexSettings());
                //client.create
            }

            if (operation.Equals("delete"))
            {
                //client.DeleteById(indexName, "documents", document.DocumentId);
                IDeleteByQueryRequest iDeleteByQueryRequest = new DeleteByQueryRequest();
                //IDeleteIndexRequest delReq = new DeleteIndexRequest(indexName);
                //client.DeleteIndex()
                //client.DeleteByQuery(new DeleteByQueryRequest())
                client.Delete<DocumentModel>(f => f.Id(document.DocumentId).Index(indexName).Refresh());
                //var response = this.Client.Delete<ElasticsearchProject>(f=>f.Id(newDocument.Id).Index(newIndex).Refresh());
            }
            else
            {
               //IIndexRequest<DocumentModel> indexRequest = IndexRequest<DocumentModel>();
               //client.Index(i)
               //client.Index(document, indexName, "documents", document.DocumentId);
               //IndexDescriptor indexDesc = IndexDescriptor;
               //IndexRequestParameters indexParameter = new IndexRequestParameters();
               // indexParameter.Replication(1);
                client.Index(document, i => i.Id(document.DocumentId).Index(indexName));
                //client.Index();
                //client.Index()
            }
        }
Beispiel #5
0
        private void CreateIndex(string indexName)
        {
            if(_client.IndexExists(indexName).Exists)
                return;

            var createIndexRequest = new CreateIndexRequest(indexName)
            {
                IndexSettings = new IndexSettings()
                {
                    Settings =
                    {
                        {"index.store.compress.stored", true},
                        {"index.store.compress.tv", true},
                        {"index.query.default_field", ElasticSearchFields.Message}
                    }
                }
            };

            IIndicesOperationResponse result = _client.CreateIndex(createIndexRequest);

            CreateMappings(indexName);

            if (!result.ConnectionStatus.Success)
            {
                throw new ApplicationException(string.Format("Failed to create index: '{0}'. Result: '{1}'", indexName, result.ConnectionStatus.ResponseRaw));
            }

            Log.Trace("{0}: Index '{1}' i successfully created.", LogContext.LogType, indexName);
        }