Exemplo n.º 1
0
 private string GetIndexPattern(ElasticClient client, Type type = null)
 {
     if (UseTypeIndex)
     {
         return(type.IsNotNull() ? ServerHelpers.CreateIndexName(client.TenantId, type.FullName) : ServerHelpers.CreateIndexName <T>(client.TenantId));
     }
     return(string.IsNullOrEmpty(client.TenantId) ? "_all" : $"{client.TenantId}_*");
 }
Exemplo n.º 2
0
        private static SearchResult <T2> Do <T2>(ElasticClient client, Query <T> query, string path, JsonSerializerSettings jsonSettings, HttpMethod httpMethod)
            where T2 : class
        {
            var response = client.Client.DoRequest <StringResponse>(httpMethod, $"{ServerHelpers.CreateIndexName<T2>(client.TenantId)}/{path}", query.ToJson());

            if (typeof(T).IsCastableTo(typeof(T2), true))
            {
                JsonConvert.DeserializeObject <SearchResult <T2> >(response.Body, jsonSettings);
            }

            return(null);
        }
Exemplo n.º 3
0
        public void TestInitialize1()
        {
            var faker = new Faker();
            var itemCountGenerator = new RandomGenerator();

            var customers = Builder <Customer> .CreateListOfSize(1000)
                            .All()
                            .With(c => c.FirstName       = faker.Name.FirstName())
                            .With(c => c.LastName        = faker.Name.LastName())
                            .With(c => c.TelephoneNumber = faker.Phone.PhoneNumber())
                            .With(c => c.Id      = Guid.NewGuid().ToString())
                            .With(c => c.Created = faker.Date.Past())
                            .With(c => c.Age     = itemCountGenerator.Next(1, 200))
                            .Random(70)
                            .With(c => c.EmailAddress = faker.Internet.Email())
                            .Build();

            customers.ForEach(c =>
            {
                c.Products = Builder <Product> .CreateListOfSize(itemCountGenerator.Next(1, 200))
                             .All()
                             .With(oi => oi.Description = faker.Lorem.Sentence(itemCountGenerator.Next(3, 10)))
                             .With(oi => oi.Id          = Guid.NewGuid().ToString())
                             .With(oi => oi.Name        = faker.Hacker.Noun())
                             .With(oi => oi.Price       = faker.Finance.Amount())
                             .Build().ToList();
            });

            var configuration =
                IntegrationTestHelper.GetApplicationConfiguration();

            _client = new ElasticClient(configuration.Protocol, configuration.Host, configuration.Port,
                                        true, configuration.Username, configuration.Password, "test");

            var indexer = new ElasticIndexer <Customer>(_client);

            var tmap = new TypeMapping <Customer>();

            tmap.AddMapping(
                new KeyValuePair <Expression <Func <Customer, object> >, FieldTypes>(customer => customer.Products,
                                                                                     FieldTypes.Nested),
                new KeyValuePair <Expression <Func <Customer, object> >, FieldTypes>(customer => customer.FirstName,
                                                                                     FieldTypes.Keyword),
                new KeyValuePair <Expression <Func <Customer, object> >, FieldTypes>(customer => customer.LastName,
                                                                                     FieldTypes.Keyword));

            indexer.DeleteIndex(ServerHelpers.CreateIndexName <Customer>("test"));
            indexer.CreateIndex(tmap);
            indexer.IndexBulk(customers, customer => customer.Id, 100);

            Thread.Sleep(3000);
        }
Exemplo n.º 4
0
        private async Task <DocumentSearchResult <T> > DoAsync <T2>(ElasticClient client, Query <T> query, string searchPath, JsonSerializerSettings jsonSerializerSettings, HttpMethod httpMethod, bool returnQuery) where T2 : class
        {
            var indexPattern = ServerHelpers.CreateIndexName <T2>(client.TenantId);

            var rq = string.IsNullOrEmpty(query.Scroll) ? null : new SearchRequestParameters {
                Scroll = TimeSpan.FromMinutes(5)
            };
            var response = await client.Client.DoRequestAsync <StringResponse>(httpMethod, $"{indexPattern}/{searchPath}", new CancellationToken(false), query.ToJson(), rq);

            CheckResponse(response);
            var result = JsonConvert.DeserializeObject <DocumentSearchResult <T> >(response.Body, jsonSerializerSettings);

            if (returnQuery)
            {
                result.Query = query;
            }

            return(result);
        }
Exemplo n.º 5
0
 private static string GetIndexPattern <T2>(ElasticClient client)
     where T2 : class
 {
     return(string.IsNullOrEmpty(client.TenantId) ? "_all" : $"{ServerHelpers.CreateIndexName<T2>(client.TenantId)}");
 }
Exemplo n.º 6
0
        private static async Task <SearchResult <T> > DoAsync(ElasticClient client, Query <T> query, string path, JsonSerializerSettings jsonSettings, HttpMethod httpMethod, CancellationToken token)
        {
            var response = await client.Client.DoRequestAsync <StringResponse>(httpMethod, $"{ServerHelpers.CreateIndexName<T>(client.TenantId)}/{path}", token, query.ToJson());

            CheckResponse(response);
            return(JsonConvert.DeserializeObject <SearchResult <T> >(response.Body, jsonSettings));
        }
Exemplo n.º 7
0
        private static ElasticsearchResponse <string> Do(ElasticClient client, string query, string type, string path, HttpMethod httpMethod)
        {
            var response = client.Client.DoRequest <StringResponse>(httpMethod, $"{ServerHelpers.CreateIndexName(client.TenantId, type)}/{path}", query);

            return(response);
        }