public void CreateARepositoryThenGetIt()
        {
            var repository        = ElasticsearchConfiguration.NewUniqueIndexName();
            var createReposResult = this.Client.CreateRepository(repository, r => r
                                                                 .FileSystem(@"local\\path", o => o
                                                                             .Compress()
                                                                             .ConcurrentStreams(10)
                                                                             )
                                                                 );

            createReposResult.IsValid.Should().BeTrue();
            createReposResult.Acknowledged.Should().BeTrue();

            var t = this.Client.GetRepository(new GetRepositoryRequest(repository));

            t.IsValid.Should().BeTrue();
            t.Repositories.Should().NotBeEmpty();

            var repos = t.Repositories.First();

            repos.Key.Should().Be(repository);
            repos.Value.Should().NotBeNull();
            repos.Value.Type.Should().Be("fs");
            repos.Value.Settings.Should().NotBeEmpty()
            .And.HaveCount(3)
            .And.ContainKey("compress")
            .And.ContainKey("concurrent_streams")
            .And.ContainKey("location");

            var deleteReposResult = this.Client.DeleteRepository(repository);

            deleteReposResult.IsValid.Should().BeTrue();
            deleteReposResult.Acknowledged.Should().BeTrue();
        }
Пример #2
0
        public void DeleteSingleAlias()
        {
            var indexName = ElasticsearchConfiguration.NewUniqueIndexName();
            var aliasName = ElasticsearchConfiguration.NewUniqueIndexName();

            var createIndexResponse = this.Client.CreateIndex(indexName);

            createIndexResponse.IsValid.Should().BeTrue();

            var putAliasResponse = this.Client.PutAlias(a => a
                                                        .Index(indexName)
                                                        .Name(aliasName)
                                                        );

            putAliasResponse.IsValid.Should().BeTrue();

            var aliases = this.Client.GetAliasesPointingToIndex(indexName);

            aliases.Should().NotBeNull().And.HaveCount(1);

            var deleteAliasResponse = this.Client.DeleteAlias(new DeleteAliasRequest(indexName, aliasName));

            deleteAliasResponse.IsValid.Should().BeTrue();

            aliases = this.Client.GetAliasesPointingToIndex(indexName);
            aliases.Should().NotBeNull().And.HaveCount(0);
        }
Пример #3
0
        public void CreateAndDeleteRepository_ThenSnapshotWithoutWait()
        {
            var repositoryName    = ElasticsearchConfiguration.NewUniqueIndexName();
            var createReposResult = this.Client.CreateRepository(repositoryName, r => r
                                                                 .FileSystem(@"local\\path", o => o
                                                                             .Compress()
                                                                             .ConcurrentStreams(10)
                                                                             )
                                                                 );

            createReposResult.IsValid.Should().BeTrue();
            createReposResult.Acknowledged.Should().BeTrue();

            var backupName       = ElasticsearchConfiguration.NewUniqueIndexName();
            var snapshotResponse = this.Client.Snapshot(repositoryName, backupName, selector: f => f
                                                        .Index(ElasticsearchConfiguration.NewUniqueIndexName())
                                                        .IgnoreUnavailable()
                                                        .Partial());

            snapshotResponse.IsValid.Should().BeTrue();
            snapshotResponse.Accepted.Should().BeTrue();
            snapshotResponse.Snapshot.Should().BeNull();

            var deleteSnapshotReponse = this.Client.DeleteSnapshot(repositoryName, backupName);

            deleteSnapshotReponse.IsValid.Should().BeTrue();

            var deleteReposResult = this.Client.DeleteRepository(repositoryName);

            deleteReposResult.IsValid.Should().BeTrue();
            deleteReposResult.Acknowledged.Should().BeTrue();
        }
Пример #4
0
        public void AnalyzedReturnsMoreItems()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();
            var x     = this._client.CreateIndex(index, s => s
                                                 .AddMapping <ElasticsearchProject>(m => m
                                                                                    .MapFromAttributes()
                                                                                    .Properties(pp => pp
                                                                                                .String(pps => pps.Name(p => p.Country).Index(FieldIndexOption.analyzed))
                                                                                                )
                                                                                    )
                                                 );

            Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());

            var indexResult = this._client.Index(
                new ElasticsearchProject
            {
                Country = "The Royal Kingdom Of The Netherlands"
            }, i => i.Index(index).Refresh()
                );

            Assert.IsTrue(indexResult.IsValid);

            var result = this._client.Search <ElasticsearchProject>(s => s
                                                                    .Index(index)
                                                                    .FacetTerm(ft => ft.OnField(f => f.Country))
                                                                    .MatchAll()
                                                                    );
            var facets = result.FacetItems <TermItem>(f => f.Country);

            Assert.AreEqual(5, facets.Count());
            facets.Select(f => f.Term).Should().Contain("royal");
        }
        public void WhenPostExceedsHttpLimit_DoNotRetry_UsingConnectionPooling()
        {
            var pool = new StaticConnectionPool(new []
            {
                new Uri("http://localhost:9200"),
                new Uri("http://127.0.0.1:9200"),
            });
            var settings = new ConnectionSettings(pool);
            var client   = new ElasticClient(settings);


            var index     = ElasticsearchConfiguration.NewUniqueIndexName();
            var projects  = NestTestData.Data;
            var people    = NestTestData.People;
            var boolTerms = NestTestData.BoolTerms;
            var bulk      = client.Bulk(b => b
                                        .FixedPath(index)
                                        .IndexMany(projects)
                                        .IndexMany(people)
                                        .IndexMany(boolTerms)
                                        );

            bulk.IsValid.Should().BeFalse();
            bulk.ConnectionStatus.NumberOfRetries.Should().Be(0);
        }
Пример #6
0
        public void CreateIndexWithWarmer()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();

            var result = this.Client.CreateIndex(index, c => c
                                                 .NumberOfReplicas(0)
                                                 .NumberOfShards(1)
                                                 .AddWarmer(wd => wd
                                                            .Type <ElasticsearchProject>()
                                                            .WarmerName("warmer_createindexwithwarmer")
                                                            .Search <ElasticsearchProject>(s => s
                                                                                           .Query(q => q
                                                                                                  .Term(p => p.Name, "strange-value")
                                                                                                  )
                                                                                           )
                                                            )
                                                 );

            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.ConnectionStatus.Should().NotBeNull();

            var warmerResult = this.Client.GetWarmer("warmer_createindexwithwarmer", w => w
                                                     .Name("warmer_createindexwithwarmer")
                                                     );

            warmerResult.IsValid.Should().BeTrue();
            warmerResult.Indices.Should().NotBeNull();
            warmerResult.Indices.Should().ContainKey(index);
            warmerResult.Indices[index].Should().ContainKey("warmer_createindexwithwarmer");
            var warmerMapping = warmerResult.Indices[index]["warmer_createindexwithwarmer"];

            warmerMapping.Name.Should().Be("warmer_createindexwithwarmer");
            //warmerMapping.Source.Should().Contain("\"strange-value\"");
        }
Пример #7
0
        public void AliasWithFilterPointingToIndex()
        {
            var indexName           = ElasticsearchConfiguration.NewUniqueIndexName();
            var aliasName           = ElasticsearchConfiguration.NewUniqueIndexName();
            var createIndexResponse = this.Client.CreateIndex(indexName, c => c
                                                              .NumberOfReplicas(0)
                                                              .NumberOfShards(1)
                                                              );

            createIndexResponse.IsValid.Should().BeTrue();

            var aliasResponse = this.Client.Alias(a => a
                                                  .Add(aa => aa
                                                       .Alias(aliasName)
                                                       .IndexRouting("1")
                                                       .Filter <dynamic>(f => f.Term("foo", "bar")
                                                                         )
                                                       )
                                                  );

            aliasResponse.IsValid.Should().BeTrue();

            var aliases = this.Client.GetAliasesPointingToIndex(indexName);

            aliases.Should().NotBeNull().And.HaveCount(1);
            var alias  = aliases.First();
            var filter = alias.Filter;

            filter.Should().NotBeNull();
            var term = filter.Term;

            term.Should().NotBeNull();
            term.Field.Should().Be("foo");
            term.Value.Should().Be("bar");
        }
Пример #8
0
        public void ReindexMinimal()
        {
            var toIndex    = ElasticsearchConfiguration.NewUniqueIndexName();
            var observable = this.Client.Reindex <object>(r => r
                                                          .FromIndex(ElasticsearchConfiguration.DefaultIndex)
                                                          .ToIndex(toIndex)
                                                          );
            var observer = new ReindexObserver <object>(
                onError: (e) => Assert.Fail(e.Message),
                completed: () =>
            {
                var refresh            = this.Client.Refresh(r => r.Indices(toIndex, ElasticsearchConfiguration.DefaultIndex));
                var originalIndexCount = this.Client.Count(c => c
                                                           .Index(ElasticsearchConfiguration.DefaultIndex)
                                                           .Query(q => q.MatchAll())
                                                           );
                var newIndexCount = this.Client.Count(c => c
                                                      .Index(toIndex)
                                                      .Query(q => q.MatchAll())
                                                      );
                Assert.Greater(newIndexCount.Count, 0);
                Assert.AreEqual(originalIndexCount.Count, newIndexCount.Count);
            }
                );

            observable.Subscribe(observer);
        }
Пример #9
0
        public void FluentMappingReturnsResults()
        {
            var indexName = ElasticsearchConfiguration.NewUniqueIndexName();

            this.Client.CreateIndex(indexName, settings => settings
                                    .Settings(s => s.Add("search.slowlog.threshold.fetch.warn", "1s"))
                                    .Analysis(x =>
            {
                var descriptor = x.Analyzers(i => i.Add("autocomplete", new CustomAnalyzer
                {
                    Tokenizer = new WhitespaceTokenizer().Type,
                    Filter    = new[] { new LowercaseTokenFilter().Type, "engram" }
                }));

                descriptor.TokenFilters(i => i.Add("engram", new EdgeNGramTokenFilter
                {
                    MinGram = 3,
                    MaxGram = 10
                }
                                                   ));

                return(descriptor);
            })
                                    .AddMapping <TechnicalProduct>(m => MapTechnicalProduct(m, indexName)));

            var index = this.Client.GetIndexSettings(i => i.Index(indexName));
        }
Пример #10
0
        public void CreateIndexShouldNotThrowNullReference()
        {
            var settings = new IndexSettings();

            settings.NumberOfReplicas = 1;
            settings.NumberOfShards   = 5;
            settings.Settings.Add("index.refresh_interval", "10s");
            settings.Settings.Add("merge.policy.merge_factor", "10");
            settings.Settings.Add("search.slowlog.threshold.fetch.warn", "1s");
            settings.Analysis.Analyzers.Add(new KeyValuePair <string, AnalyzerBase>("keyword", new KeywordAnalyzer()));
            settings.Analysis.Analyzers.Add(new KeyValuePair <string, AnalyzerBase>("simple", new SimpleAnalyzer()));
            settings.Mappings.Add(new RootObjectMapping
            {
                Name       = "my_root_object",
                Properties = new Dictionary <PropertyNameMarker, IElasticType>
                {
                    { "my_field", new StringMapping()
                      {
                          Name = "my_string_field "
                      } }
                }
            });

            Assert.DoesNotThrow(() =>
            {
                var idxRsp = this.Client.CreateIndex(ElasticsearchConfiguration.NewUniqueIndexName(), i => i.InitializeUsing(settings));
                Assert.IsTrue(idxRsp.IsValid, idxRsp.ConnectionStatus.ToString());
            });
        }
        public async void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerException_Async()
        {
            var uris = new []
            {
                ElasticsearchConfiguration.CreateBaseUri(9200),
                ElasticsearchConfiguration.CreateBaseUri(9200),
                ElasticsearchConfiguration.CreateBaseUri(9200),
            };
            var connectionPool = new StaticConnectionPool(uris);
            var client         = new ElasticClient(new ConnectionSettings(connectionPool)
                                                   .ThrowOnElasticsearchServerExceptions()
                                                   .SetTimeout(1000)
                                                   );

            try
            {
                var index  = ElasticsearchConfiguration.NewUniqueIndexName();
                var create = await client.CreateIndexAsync(i => i.Index(index));

                var close = await client.CloseIndexAsync(i => i.Index(index));

                var result = await client.SearchAsync <ElasticsearchProject>(s => s.Index(index));
            }
            catch (ElasticsearchServerException)
            {
                Assert.Pass("ElasticearchServerException caught");
            }
            catch (Exception e)
            {
                Assert.Fail("Did not expect exception of type {0} to be caught", e.GetType().Name);
            }
        }
Пример #12
0
        public void PercolateTypedDocWithQuery()
        {
            var c    = this._client;
            var name = "eclecticsearch" + ElasticsearchConfiguration.NewUniqueIndexName();
            var re   = c.UnregisterPercolator(name, ur => ur.Index <ElasticsearchProject>());
            var r    = c.RegisterPercolator <ElasticsearchProject>(name, p => p
                                                                   .AddMetadata(md => md.Add("color", "blue"))
                                                                   .Query(q => q
                                                                          .Term(f => f.Country, "netherlands")
                                                                          )
                                                                   );

            Assert.True(r.IsValid);
            Assert.True(r.Created);
            c.Refresh();
            var obj = new ElasticsearchProject()
            {
                Name    = "NEST",
                Country = "netherlands",
                LOC     = 100000,             //Too many :(
            };
            var percolateResponse = this._client.Percolate(obj, p => p.Query(q => q.Match(m => m.OnField("color").Query("blue"))));

            Assert.True(percolateResponse.IsValid);
            Assert.NotNull(percolateResponse.Matches);
            Assert.True(percolateResponse.Matches.Select(m => m.Id).Contains(name));

            //should not match since we registered with the color blue
            percolateResponse = this._client.Percolate(obj, p => p.Query(q => q.Term("color", "green")));
            Assert.True(percolateResponse.IsValid);
            Assert.NotNull(percolateResponse.Matches);
            Assert.False(percolateResponse.Matches.Select(m => m.Id).Contains(name));

            re = c.UnregisterPercolator(name, ur => ur.Index <ElasticsearchProject>());
        }
Пример #13
0
        public void NotAnalyzedReturnsOneItem()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();
            var x     = this._client.CreateIndex(index, s => s
                                                 .AddMapping <ElasticsearchProject>(m => m.MapFromAttributes())
                                                 );

            Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());

            var typeMappingResponse = this._client.GetMapping(gm => gm.Index(index).Type("elasticsearchprojects"));
            var typeMapping         = typeMappingResponse.Mapping;
            var mapping             = typeMapping.Properties["country"] as StringMapping;

            Assert.NotNull(mapping);
            Assert.AreEqual(FieldIndexOption.not_analyzed, mapping.Index);

            var indexResult = this._client.Index(new ElasticsearchProject
            {
                Country = "The Royal Kingdom Of The Netherlands"
            }, i => i.Index(index).Refresh());

            Assert.IsTrue(indexResult.IsValid);

            var result = this._client.Search <ElasticsearchProject>(s => s
                                                                    .Index(index)
                                                                    .FacetTerm(ft => ft.OnField(f => f.Country))
                                                                    .MatchAll()
                                                                    );
            var facets = result.FacetItems <TermItem>(f => f.Country);

            Assert.AreEqual(1, facets.Count());
            Assert.AreEqual("The Royal Kingdom Of The Netherlands", facets.FirstOrDefault().Term);
        }
Пример #14
0
        public void PutSingleAlias()
        {
            var indexName = ElasticsearchConfiguration.NewUniqueIndexName();
            var aliasName = ElasticsearchConfiguration.NewUniqueIndexName();

            var createIndexResponse = this.Client.CreateIndex(indexName);

            createIndexResponse.IsValid.Should().BeTrue();

            var result = this.Client.PutAlias(a => a
                                              .Index(indexName)
                                              .Name(aliasName)
                                              .Filter <ElasticsearchProject>(f => f
                                                                             .Term(p => p.Name, "nest")
                                                                             )
                                              );

            result.IsValid.Should().BeTrue();

            var aliases = this.Client.GetAliasesPointingToIndex(indexName);

            aliases.Should().NotBeNull().And.HaveCount(1);
            var alias = aliases.First();

            alias.Name.ShouldAllBeEquivalentTo(aliasName);
            alias.Filter.Should().NotBeNull();
            alias.Filter.Term.Field.Should().Be("name");
            alias.Filter.Term.Value.Should().Be("nest");
        }
Пример #15
0
        public void Setup()
        {
            _indexName      = ElasticsearchConfiguration.NewUniqueIndexName();
            _repositoryName = ElasticsearchConfiguration.NewUniqueIndexName();
            _snapshotName   = ElasticsearchConfiguration.NewUniqueIndexName();

            var descriptor = new BulkDescriptor();

            _indexedElements = new List <ElasticsearchProject>();

            for (int i = 0; i < 100; i++)
            {
                var elementToIndex = new ElasticsearchProject()
                {
                    Id      = i,
                    Name    = "Coboles",
                    Content = "COBOL elasticsearch client"
                };
                descriptor = descriptor.Index <ElasticsearchProject>(d => d.Index(_indexName).Document(elementToIndex));
                _indexedElements.Add(elementToIndex);
            }

            var bulkResponse = this.Client.Bulk(d => descriptor);

            this.Client.CreateRepository(_repositoryName, r => r
                                         .FileSystem(@"local\\path", o => o
                                                     .Compress()
                                                     .ConcurrentStreams(10)));
        }
        public void CreateAndValidateAndDeleteRepository_ThenSnapshotWithWait()
        {
            var repositoryName    = ElasticsearchConfiguration.NewUniqueIndexName();
            var createReposResult = this.Client.CreateRepository(repositoryName, r => r
                                                                 .FileSystem(@"local\\path", o => o
                                                                             .Compress()
                                                                             .ConcurrentStreams(10)
                                                                             )
                                                                 );

            createReposResult.IsValid.Should().BeTrue();
            createReposResult.Acknowledged.Should().BeTrue();

            // Repository verification added in ES 1.4
            if (ElasticsearchConfiguration.CurrentVersion > new Version("1.3.9"))
            {
                var validateResponse = this.Client.VerifyRepository(new VerifyRepositoryRequest(repositoryName));
                validateResponse.IsValid.Should().BeTrue();

                validateResponse.Nodes.Should().NotBeEmpty();
                var kv = validateResponse.Nodes.First();
                kv.Key.Should().NotBeNullOrWhiteSpace();
                kv.Value.Should().NotBeNull();
                kv.Value.Name.Should().NotBeNullOrWhiteSpace();
            }

            var backupName       = ElasticsearchConfiguration.NewUniqueIndexName();
            var snapshotResponse = this.Client.Snapshot(repositoryName, backupName, selector: f => f
                                                        .WaitForCompletion(true)
                                                        .Index(ElasticsearchConfiguration.NewUniqueIndexName())
                                                        .IgnoreUnavailable()
                                                        .Partial());

            snapshotResponse.IsValid.Should().BeTrue();
            snapshotResponse.Accepted.Should().BeTrue();
            snapshotResponse.Snapshot.Should().NotBeNull();
            snapshotResponse.Snapshot.EndTimeInMilliseconds.Should().BeGreaterThan(0);
            snapshotResponse.Snapshot.StartTime.Should().BeAfter(DateTime.UtcNow.AddDays(-1));

            var getSnapshotResponse = this.Client.GetSnapshot(repositoryName, backupName);

            getSnapshotResponse.IsValid.Should().BeTrue();
            getSnapshotResponse.Snapshots.Should().NotBeEmpty();
            var snapShot = getSnapshotResponse.Snapshots.First();

            snapShot.StartTime.Should().BeAfter(DateTime.UtcNow.AddDays(-1));

            var getAllSnapshotResponse = this.Client.GetSnapshot(repositoryName, "_all");

            getAllSnapshotResponse.IsValid.Should().BeTrue();
            getAllSnapshotResponse.Snapshots.Should().NotBeEmpty();
            getAllSnapshotResponse.Snapshots.Count().Should().BeGreaterOrEqualTo(1);

            var deleteReposResult = this.Client.DeleteRepository(repositoryName);

            deleteReposResult.IsValid.Should().BeTrue();
            deleteReposResult.Acknowledged.Should().BeTrue();
        }
Пример #17
0
        public void GetDifferentMappingsFromMultipleIndices()
        {
            var indices = new[]
            {
                ElasticsearchConfiguration.NewUniqueIndexName(),
                ElasticsearchConfiguration.NewUniqueIndexName()
            };

            var x = this.Client.CreateIndex(indices.First(), s => s
                                            .AddMapping <ElasticsearchProject>(m => m.MapFromAttributes())
                                            .AddMapping <Person>(m => m.MapFromAttributes())
                                            );

            Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());

            x = this.Client.CreateIndex(indices.Last(), s => s
                                        .AddMapping <ElasticsearchProject>(m => m.MapFromAttributes())
                                        .AddMapping <MockData.Domain.CustomGeoLocation>(m => m.MapFromAttributes())
                                        );
            Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());

            var response = this.Client.GetMapping(new GetMappingRequest(string.Join(",", indices), "*"));

            response.Should().NotBeNull();
            response.Mappings.Should().NotBeEmpty()
            .And.HaveCount(2);
            foreach (var indexMapping in response.Mappings)
            {
                var indexName = indexMapping.Key;
                indices.Should().Contain(indexName);
                var mappings = indexMapping.Value;
                mappings.Should().NotBeEmpty().And.HaveCount(2);
                mappings.Should().Contain(m => m.TypeName == "elasticsearchprojects")
                .And.Contain(m => m.TypeName == (indexName == indices.First() ? "person" : "customgeolocation"));
                foreach (var mapping in mappings)
                {
                    switch (mapping.TypeName)
                    {
                    case "elasticsearchprojects":
                        TestElasticsearchProjectMapping(mapping.Mapping);
                        break;

                    case "person":
                        TestPersonMapping(mapping.Mapping);
                        break;

                    case "customgeolocation":
                        break;

                    default:
                        Assert.Fail("Unexpected mapping found {0}", mapping.TypeName);
                        break;
                    }
                }
            }
        }
Пример #18
0
        public void IndicesPointingToAlias()
        {
            var aliasName = ElasticsearchConfiguration.NewUniqueIndexName();

            var indexName1           = ElasticsearchConfiguration.NewUniqueIndexName();
            var createIndexResponse1 = this.Client.CreateIndex(indexName1);

            createIndexResponse1.IsValid.Should().BeTrue();

            var aliasResponse1 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName1)
                                                        .Alias(aliasName)
                                                        .IndexRouting("1")
                                                        )
                                                   );

            aliasResponse1.IsValid.Should().BeTrue();

            var indexName2           = ElasticsearchConfiguration.NewUniqueIndexName();
            var createIndexResponse2 = this.Client.CreateIndex(indexName2);

            createIndexResponse2.IsValid.Should().BeTrue();

            var aliasResponse2 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName2)
                                                        .Alias(aliasName)
                                                        .IndexRouting("1")
                                                        )
                                                   );

            aliasResponse2.IsValid.Should().BeTrue();

            var indexName3           = ElasticsearchConfiguration.NewUniqueIndexName();
            var createIndexResponse3 = this.Client.CreateIndex(indexName3);

            createIndexResponse3.IsValid.Should().BeTrue();

            var aliasResponse3 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName3)
                                                        .Alias(aliasName)
                                                        .IndexRouting("1")
                                                        )
                                                   );

            aliasResponse3.IsValid.Should().BeTrue();

            var indices = this.Client.GetIndicesPointingToAlias(aliasName);

            indices.Should().NotBeNull().And.HaveCount(3);
            indices.ShouldAllBeEquivalentTo(new[] { indexName1, indexName2, indexName3 });
        }
Пример #19
0
        public void SimpleMapByAttributes()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();
            var x     = this._client.CreateIndex(index, s => s
                                                 .AddMapping <ElasticSearchProject>(m => m.MapFromAttributes())
                                                 );

            Assert.IsTrue(x.OK, x.ConnectionStatus.ToString());
            Assert.IsTrue(x.OK);

            var typeMapping = this._client.GetMapping(index, "elasticsearchprojects");

            TestMapping(typeMapping);
        }
Пример #20
0
        public void SimpleMapByAttributes()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();
            var x     = this._client.CreateIndex(index, s => s
                                                 .AddMapping <ElasticsearchProject>(m => m.MapFromAttributes())
                                                 );

            Assert.IsTrue(x.Acknowledged, x.ConnectionStatus.ToString());

            var typeMapping = this._client.GetMapping(i => i.Index(index).Type("elasticsearchprojects"));

            typeMapping.Should().NotBeNull();
            TestMapping(typeMapping.Mapping);
        }
Пример #21
0
        public void AliasesPointingToIndex()
        {
            var indexName = ElasticsearchConfiguration.NewUniqueIndexName();

            var createIndexResponse = this.Client.CreateIndex(indexName, c => c
                                                              .NumberOfReplicas(0)
                                                              .NumberOfShards(1)
                                                              );

            createIndexResponse.IsValid.Should().BeTrue();

            var aliasName1 = ElasticsearchConfiguration.NewUniqueIndexName();
            var aliasName2 = ElasticsearchConfiguration.NewUniqueIndexName();
            var aliasName3 = ElasticsearchConfiguration.NewUniqueIndexName();

            var aliasResponse1 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName)
                                                        .Alias(aliasName1)
                                                        .IndexRouting("1")
                                                        )
                                                   );

            aliasResponse1.IsValid.Should().BeTrue();

            var aliasResponse2 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName)
                                                        .Alias(aliasName2)
                                                        .IndexRouting("2")
                                                        )
                                                   );

            aliasResponse2.IsValid.Should().BeTrue();

            var aliasResponse3 = this.Client.Alias(a => a
                                                   .Add(aa => aa
                                                        .Index(indexName)
                                                        .Alias(aliasName3)
                                                        .IndexRouting("3")
                                                        )
                                                   );

            aliasResponse3.IsValid.Should().BeTrue();

            var aliases = this.Client.GetAliasesPointingToIndex(indexName);

            aliases.Should().NotBeNull().And.HaveCount(3);
        }
Пример #22
0
        public AnalyzerTestResult MapAndAnalyze(
            Func <AnalysisDescriptor, AnalysisDescriptor> analysisSelector,
            Func <RootObjectMappingDescriptor <AnalyzerTest>, RootObjectMappingDescriptor <AnalyzerTest> > typeMappingDescriptor,
            string text = "ElasticSearch is yummy"
            )
        {
            var index  = ElasticsearchConfiguration.NewUniqueIndexName();
            var result = this._client.CreateIndex(index, c => c
                                                  .NumberOfReplicas(1)
                                                  .NumberOfShards(1)
                                                  .Analysis(analysisSelector)
                                                  .AddMapping <AnalyzerTest>(typeMappingDescriptor)

                                                  );

            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.OK.Should().BeTrue();
            result.Acknowledged.Should().BeTrue();

            //index a doc so we can be sure a shard is available
            this._client.Index <AnalyzerTest>(new AnalyzerTest()
            {
                Txt = text
            }, index, new IndexParameters {
                Refresh = true
            });

            var settingsResult = this._client.GetMapping <AnalyzerTest>(index);

            settingsResult.Should().NotBeNull();
            settingsResult.Properties.Should().NotBeNull();
            settingsResult.Properties["txt"].Should().NotBeNull();
            settingsResult.Properties["txt"].Type.Name.Should().NotBeNullOrEmpty().And.BeEquivalentTo("string");

            var validateResult = this._client.Analyze <AnalyzerTest>(p => p.Txt, index, text);

            validateResult.Should().NotBeNull();
            validateResult.IsValid.Should().BeTrue();
            validateResult.Tokens.Should().NotBeEmpty();

            return(new AnalyzerTestResult
            {
                AnalyzeResponse = validateResult,
                ElasticType = settingsResult.Properties["txt"]
            });
        }
Пример #23
0
        public void PercolateTypedDocWithQuery()
        {
            var c    = this._client;
            var name = "eclecticsearch" + ElasticsearchConfiguration.NewUniqueIndexName();
            var re   = c.UnregisterPercolator <ElasticSearchProject>(name);
            var r    = c.RegisterPercolator <ElasticSearchProject>(p => p
                                                                   .Name(name)
                                                                   .Add("color", "blue")
                                                                   .Query(q => q
                                                                          .Term(f => f.Country, "netherlands")
                                                                          )
                                                                   );

            Assert.True(r.IsValid);
            Assert.True(r.OK);
            var percolateResponse = this._client.Percolate <ElasticSearchProject>(p => p
                                                                                  .Object(new ElasticSearchProject()
            {
                Name    = "NEST",
                Country = "netherlands",
                LOC     = 100000,                     //Too many :(
            })
                                                                                  .Query(q => q.Term("color", "blue"))
                                                                                  );

            Assert.True(percolateResponse.IsValid);
            Assert.True(percolateResponse.OK);
            Assert.NotNull(percolateResponse.Matches);
            Assert.True(percolateResponse.Matches.Contains(name));

            //should not match since we registered with the color blue
            percolateResponse = this._client.Percolate <ElasticSearchProject>(p => p
                                                                              .Object(new ElasticSearchProject()
            {
                Name    = "NEST",
                Country = "netherlands",
                LOC     = 100000,                     //Too many :(
            })
                                                                              .Query(q => q.Term("color", "green"))
                                                                              );
            Assert.True(percolateResponse.IsValid);
            Assert.True(percolateResponse.OK);
            Assert.NotNull(percolateResponse.Matches);
            Assert.False(percolateResponse.Matches.Contains(name));

            re = c.UnregisterPercolator <ElasticSearchProject>(name);
        }
Пример #24
0
        public void UpdateMappingWithNewFieldsShouldApplyIndexOption()
        {
            var index        = ElasticsearchConfiguration.NewUniqueIndexName();
            var createResult = this.Client.CreateIndex(i => i
                                                       .Index(index)
                                                       .AddMapping <Round1Parent>(m => m.MapFromAttributes())
                                                       );

            createResult.IsValid.Should().BeTrue();

            var getMapping = this.Client.GetMapping <Round1Parent>(m => m.Index(index));

            getMapping.Mapping.Name.Should().Be("parent");
            var urlMapping = getMapping.Mapping.Properties["alternate_urls"] as ObjectMapping;

            urlMapping.Should().NotBeNull();

            var urlStringMapping = urlMapping.Properties["url"] as StringMapping;

            urlStringMapping.Should().NotBeNull();

            urlStringMapping.Index.Should().Be(FieldIndexOption.NotAnalyzed);

            urlMapping.Properties.Should().NotContainKey("type");

            var updateMapping = this.Client.Map <Round2Parent>(m => m
                                                               .Index(index)
                                                               .MapFromAttributes()
                                                               );

            updateMapping.IsValid.Should().BeTrue();

            getMapping = this.Client.GetMapping <Round1Parent>(m => m.Index(index));
            getMapping.Mapping.Name.Should().Be("parent");
            urlMapping = getMapping.Mapping.Properties["alternate_urls"] as ObjectMapping;
            urlMapping.Should().NotBeNull();

            urlStringMapping = urlMapping.Properties["url"] as StringMapping;
            urlStringMapping.Should().NotBeNull();
            urlStringMapping.Index.Should().Be(FieldIndexOption.NotAnalyzed);

            var urlTypeMapping = urlMapping.Properties["type"] as StringMapping;

            urlTypeMapping.Should().NotBeNull();
            urlTypeMapping.Index.Should().Be(FieldIndexOption.NotAnalyzed);
        }
Пример #25
0
        public void IndexWithDashesAreNotStripped2()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashes";
            var x     = this._client.CreateIndex(index);

            x.Acknowledged.Should().BeTrue();
            var alias       = ElasticsearchConfiguration.NewUniqueIndexName() + "-dashes-alias";
            var aliasResult = this._client.Alias(a => a.Add(aa => aa.Index(index).Alias(alias)));

            aliasResult.IsValid.Should().BeTrue();
            aliasResult.Acknowledged.Should().BeTrue();

            var elasticsearchClient = new ElasticsearchClient(ElasticsearchConfiguration.Settings());
            var dynamicResult       = elasticsearchClient.IndicesGetAlias(alias);

            dynamicResult.Response.ContainsKey(index).Should().BeTrue();
        }
Пример #26
0
        public AnalyzerTestResult MapAndAnalyze(
            Func <AnalysisDescriptor, AnalysisDescriptor> analysisSelector,
            Func <PutMappingDescriptor <AnalyzerTest>, PutMappingDescriptor <AnalyzerTest> > typeMappingDescriptor,
            string text = "Elasticsearch is yummy"
            )
        {
            var index  = ElasticsearchConfiguration.NewUniqueIndexName();
            var result = this._client.CreateIndex(index, c => c
                                                  .NumberOfReplicas(1)
                                                  .NumberOfShards(1)
                                                  .Analysis(analysisSelector)
                                                  .AddMapping <AnalyzerTest>(typeMappingDescriptor)

                                                  );

            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.Acknowledged.Should().BeTrue();
            result.Acknowledged.Should().BeTrue();

            //index a doc so we can be sure a shard is available
            this._client.Index <AnalyzerTest>(new AnalyzerTest()
            {
                Txt = text
            }, i => i.Index(index).Refresh(true));

            var settingsResult = this._client.GetMapping(gm => gm.Index(index).Type <AnalyzerTest>());
            var mapping        = settingsResult.Mapping;

            mapping.Should().NotBeNull();
            mapping.Properties.Should().NotBeNull();
            mapping.Properties["txt"].Should().NotBeNull();
            mapping.Properties["txt"].Type.Name.Should().NotBeNullOrEmpty().And.BeEquivalentTo("string");

            var validateResult = this._client.Analyze(a => a.Index(index).Field <AnalyzerTest>(p => p.Txt).Text(text));

            validateResult.Should().NotBeNull();
            validateResult.IsValid.Should().BeTrue();
            validateResult.Tokens.Should().NotBeEmpty();

            return(new AnalyzerTestResult
            {
                AnalyzeResponse = validateResult,
                ElasticType = mapping.Properties["txt"]
            });
        }
        public ReproduceConnectionStallsTests()
        {
            this._index = ElasticsearchConfiguration.NewUniqueIndexName();
            var people            = NestTestData.Session.List <Person>(10000).Get().ToList();
            var lotsOfPeople      = Partition(people, 1000).ToList();
            var createIndexResult = this.Client.CreateIndex(_index, c => c
                                                            .NumberOfReplicas(0)
                                                            .NumberOfShards(1)
                                                            .Settings(s => s.Add("index.refresh_interval", -1))
                                                            .AddMapping <Person>(m => m.MapFromAttributes())
                                                            );

            Parallel.ForEach(lotsOfPeople, (listOfPeople) =>
            {
                Client.IndexMany(listOfPeople, _index);
            });
            Client.UpdateSettings(u => u.Index(_index).RefreshInterval("1s"));
            Client.Refresh(r => r.Index(_index));
        }
Пример #28
0
        public void Reindex()
        {
            var toIndex    = ElasticsearchConfiguration.NewUniqueIndexName();
            var observable = this.Client.Reindex <object>(r => r
                                                          .FromIndex(ElasticsearchConfiguration.DefaultIndex)
                                                          .ToIndex(toIndex)
                                                          .Query(q => q.MatchAll())
                                                          .Scroll("10s")
                                                          .CreateIndex(c => c
                                                                       .NumberOfReplicas(0)
                                                                       .NumberOfShards(1)
                                                                       )
                                                          );
            var observer = new ReindexObserver <object>(
                onNext: (r) =>
            {
                var scrollResults = r.SearchResponse;
                var bulkResults   = r.BulkResponse;

                Assert.NotNull(scrollResults);
                Assert.NotNull(bulkResults);

                Assert.True(scrollResults.IsValid);
                Assert.True(bulkResults.IsValid);
            },
                onError: (e) => Assert.Fail(e.Message),
                completed: () =>
            {
                var refresh = this.Client.Refresh(r => r.Indices(toIndex, ElasticsearchConfiguration.DefaultIndex));

                var originalIndexCount = this.Client.Count(c => c
                                                           .Index(ElasticsearchConfiguration.DefaultIndex)
                                                           .Query(q => q.MatchAll())
                                                           );
                var newIndexCount = this.Client.Count(i => i.Index(toIndex).Query(q => q.MatchAll()));
                Assert.Greater(newIndexCount.Count, 0);
                Assert.AreEqual(originalIndexCount.Count, newIndexCount.Count);
            }
                );

            observable.Subscribe(observer);
        }
Пример #29
0
        public void DynamicMap()
        {
            var index = ElasticsearchConfiguration.NewUniqueIndexName();
            var x     = this._client.CreateIndex(index, s => s);

            Assert.IsTrue(x.OK, x.ConnectionStatus.ToString());
            var typeMapping = this._client.GetMapping(ElasticsearchConfiguration.DefaultIndex, "elasticsearchprojects");
            var mapping     = typeMapping.Properties["country"] as StringMapping;

            Assert.NotNull(mapping);
            mapping.Boost = 3;
            typeMapping.TypeNameMarker = "elasticsearchprojects2";
            this._client.Map(typeMapping, index);

            typeMapping = this._client.GetMapping(index, "elasticsearchprojects2");
            var countryMapping = typeMapping.Properties["country"] as StringMapping;

            Assert.NotNull(countryMapping);
            Assert.AreEqual(3, countryMapping.Boost);
        }
Пример #30
0
        public void GetAndUpdateMultiFieldMap()
        {
            var indexName = ElasticsearchConfiguration.NewUniqueIndexName();

            var indexCreateResponse = this.Client.CreateIndex(indexName);

            Assert.IsTrue(indexCreateResponse.Acknowledged, indexCreateResponse.ConnectionStatus.ToString());

            var mapResponse = this.Client.Map <ElasticsearchProject>(m => m
                                                                     .Index(indexName)
                                                                     .Properties(props => props
                                                                                 .MultiField(s => s
                                                                                             .Name(p => p.Name)
                                                                                             .Fields(pprops => pprops
                                                                                                     .String(ps => ps.Name(p => p.Name).Index(FieldIndexOption.NotAnalyzed))
                                                                                                     .String(ps => ps.Name(p => p.Name.Suffix("searchable")).Index(FieldIndexOption.Analyzed))
                                                                                                     )
                                                                                             )
                                                                                 )
                                                                     );

            mapResponse.Should().NotBeNull();
            Assert.IsTrue(mapResponse.IsValid, mapResponse.ConnectionStatus.ToString());

            var getMapResponse = this.Client.GetMapping <ElasticsearchProject>(m => m.Index(indexName));

            getMapResponse.Should().NotBeNull();
            Assert.IsTrue(getMapResponse.IsValid, getMapResponse.ConnectionStatus.ToString());
            getMapResponse.Mapping.Should().NotBeNull();

            mapResponse = this.Client.Map <ElasticsearchProject>(p => p
                                                                 .Index(indexName)
                                                                 .InitializeUsing(getMapResponse.Mapping)
                                                                 );
            mapResponse.Should().NotBeNull();
            Assert.IsTrue(mapResponse.IsValid, mapResponse.ConnectionStatus.ToString());

            var indexDeleteResponse = this.Client.DeleteIndex(x => x.Index(indexName));

            Assert.IsTrue(indexDeleteResponse.Acknowledged, indexDeleteResponse.ConnectionStatus.ToString());
        }