예제 #1
0
        public virtual void Index(string scope, string documentType, IDocument document)
        {
            var core = GetCoreName(scope, documentType);

            if (!_pendingDocuments.ContainsKey(core))
            {
                _pendingDocuments.Add(core, new List <ESDocument>());
            }

            string mapping = null;

            if (!_mappings.ContainsKey(core))
            {
                // Get mapping info
                if (Client.IndexExists(new IndexExistsCommand(scope)))
                {
                    mapping = GetMappingFromServer(scope, documentType, core);
                }
            }
            else
            {
                mapping = _mappings[core];
            }

            var submitMapping = false;

            var properties    = new Properties <ESDocument>();
            var localDocument = new ESDocument();

            for (var index = 0; index < document.FieldCount; index++)
            {
                var field = document[index];

                var key = field.Name.ToLower();

                if (localDocument.ContainsKey(key))
                {
                    var newValues = new List <object>();

                    var currentValue  = localDocument[key];
                    var currentValues = currentValue as object[];

                    if (currentValues != null)
                    {
                        newValues.AddRange(currentValues);
                    }
                    else
                    {
                        newValues.Add(currentValue);
                    }

                    newValues.AddRange(field.Values);
                    localDocument[key] = newValues.ToArray();
                }
                else
                {
                    if (string.IsNullOrEmpty(mapping) || !mapping.Contains(string.Format("\"{0}\"", field.Name)))
                    {
                        var type = field.Value != null?field.Value.GetType() : typeof(object);

                        if (type == typeof(decimal))
                        {
                            type = typeof(double);
                        }

                        var elasticType = ElasticCoreTypeMapper.GetElasticType(type);

                        var propertyMap = new CustomPropertyMap <ESDocument>(field.Name, elasticType)
                                          .Store(field.ContainsAttribute(IndexStore.Yes))
                                          .When(field.ContainsAttribute(IndexType.NotAnalyzed), p => p.Index(IndexState.not_analyzed))
                                          .When(field.Name.StartsWith("__content", StringComparison.OrdinalIgnoreCase), p => p.Analyzer(_indexAnalyzer))
                                          .When(Regex.Match(field.Name, "__content_en.*").Success, x => x.Analyzer("english"))
                                          .When(Regex.Match(field.Name, "__content_de.*").Success, x => x.Analyzer("german"))
                                          .When(Regex.Match(field.Name, "__content_ru.*").Success, x => x.Analyzer("russian"))
                                          .When(field.ContainsAttribute(IndexType.No), p => p.Index(IndexState.no));

                        properties.CustomProperty(field.Name, p => propertyMap);

                        submitMapping = true;
                    }

                    if (field.Values.Length > 1)
                    {
                        localDocument.Add(key, field.Values);
                    }
                    else
                    {
                        localDocument.Add(key, field.Value);
                    }
                }
            }

            // submit mapping
            if (submitMapping)
            {
                //Use ngrams analyzer for search in the middle of word
                //http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ngrams-compound-words.html
                var settings = new IndexSettingsBuilder()
                               .Analysis(als => als
                                         .Filter(f => f.NGram("trigrams_filter", ng => ng
                                                              .MinGram(3)
                                                              .MaxGram(20)))
                                         .Analyzer(a => a
                                                   .Custom(_indexAnalyzer, custom => custom
                                                           .Tokenizer(DefaultTokenizers.standard)
                                                           .Filter(DefaultTokenFilters.lowercase.ToString(), "trigrams_filter"))
                                                   .Custom(SearchAnalyzer, custom => custom
                                                           .Tokenizer(DefaultTokenizers.standard)
                                                           .Filter(DefaultTokenFilters.lowercase.ToString()))))
                               .Build();

                if (!Client.IndexExists(new IndexExistsCommand(scope)))
                {
                    var response = Client.CreateIndex(new IndexCommand(scope), settings);
                    _settingsUpdated = true;
                    if (response.error != null)
                    {
                        throw new IndexBuildException(response.error);
                    }
                }
                else if (!_settingsUpdated)
                {
                    // We can't update settings on active index.
                    // So we need to close it, then update settings and then open index back.
                    Client.Close(new CloseCommand(scope));
                    Client.UpdateSettings(new UpdateSettingsCommand(scope), settings);
                    Client.Open(new OpenCommand(scope));
                    _settingsUpdated = true;
                }

                var mapBuilder = new MapBuilder <ESDocument>();
                var mappingNew = mapBuilder.RootObject(documentType, d => d.Properties(p => properties)).Build();

                var result = Client.PutMapping(new PutMappingCommand(scope, documentType), mappingNew);
                if (!result.acknowledged && result.error != null)
                {
                    throw new IndexBuildException(result.error);
                }

                GetMappingFromServer(scope, documentType, core);
            }

            _pendingDocuments[core].Add(localDocument);

            // Auto commit changes when limit is reached
            if (AutoCommit && _pendingDocuments[core].Count > AutoCommitCount)
            {
                Commit(scope);
            }
        }
        public virtual void Index(string scope, string documentType, IDocument document)
        {
            var core = GetCoreName(scope, documentType);
            if (!_pendingDocuments.ContainsKey(core))
            {
                _pendingDocuments.Add(core, new List<ESDocument>());
            }

            string mapping = null;
            if (!_mappings.ContainsKey(core))
            {
                // Get mapping info
                if (Client.IndexExists(new IndexExistsCommand(scope)))
                {
                    mapping = GetMappingFromServer(scope, documentType, core);
                }
            }
            else
            {
                mapping = _mappings[core];
            }

            var submitMapping = false;

            var properties = new Properties<ESDocument>();
            var localDocument = new ESDocument();

            for (var index = 0; index < document.FieldCount; index++)
            {
                var field = document[index];

                var key = field.Name.ToLower();

                if (localDocument.ContainsKey(key))
                {
                    var objTemp = localDocument[key];
                    object[] objListTemp;
                    var temp = objTemp as object[];
                    if (temp != null)
                    {
                        var objList = new List<object>(temp) { field.Value };
                        objListTemp = objList.ToArray();
                    }
                    else
                    {
                        objListTemp = new[] { objTemp, field.Value };
                    }

                    localDocument[key] = objListTemp;
                }
                else
                {
                    if (String.IsNullOrEmpty(mapping) || !mapping.Contains(String.Format("\"{0}\"", field.Name)))
                    {
                        var type = field.Value != null ? field.Value.GetType() : null;
                        var propertyMap = new CustomPropertyMap<ESDocument>(field.Name, type)
                        .Store(field.ContainsAttribute(IndexStore.Yes))
                        .When(field.ContainsAttribute(IndexType.NotAnalyzed), p => p.Index(IndexState.not_analyzed))
                        .When(field.Name.StartsWith("__content", StringComparison.OrdinalIgnoreCase), p => p.Analyzer(SearchAnalyzer))
                        .When(Regex.Match(field.Name, "__content_en.*").Success, x => x.Analyzer("english"))
                        .When(Regex.Match(field.Name, "__content_de.*").Success, x => x.Analyzer("german"))
                        .When(Regex.Match(field.Name, "__content_ru.*").Success, x => x.Analyzer("russian"))
                        .When(field.ContainsAttribute(IndexType.No), p => p.Index(IndexState.no));

                        properties.CustomProperty(field.Name, p => propertyMap);

                        submitMapping = true;
                    }

                    localDocument.Add(key, field.Value);
                }
            }

            // submit mapping
            if (submitMapping)
            {
                //Use ngrams analyzer for search in the middle of word
                //http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/ngrams-compound-words.html
                var settings = new IndexSettingsBuilder()
                    .Analysis(als => als
                        .Analyzer(a => a.Custom(SearchAnalyzer, custom => custom
                            .Tokenizer(DefaultTokenizers.standard)
                            .Filter("trigrams_filter", DefaultTokenFilters.lowercase.ToString())))
                        .Filter(f => f.NGram("trigrams_filter", ng => ng
                                .MinGram(3)
                                .MaxGram(3)))).Build();

                if (!Client.IndexExists(new IndexExistsCommand(scope)))
                {
                    var response = Client.CreateIndex(new IndexCommand(scope), settings);
                    _settingsUpdated = true;
                    if (response.error != null)
                        throw new IndexBuildException(response.error);
                }
                else if (!_settingsUpdated)
                {
                    // We can't update settings on active index.
                    // So we need to close it, then update settings and then open index back.
                    Client.Close(new CloseCommand(scope));
                    Client.UpdateSettings(new UpdateSettingsCommand(scope), settings);
                    Client.Open(new OpenCommand(scope));
                    _settingsUpdated = true;
                }

                var mapBuilder = new MapBuilder<ESDocument>();
                var mappingNew = mapBuilder.RootObject(documentType, d => d.Properties(p => properties)).Build();

                var result = Client.PutMapping(new PutMappingCommand(scope, documentType), mappingNew);
                if (!result.acknowledged && result.error != null)
                    throw new IndexBuildException(result.error);

                GetMappingFromServer(scope, documentType, core);
            }

            _pendingDocuments[core].Add(localDocument);

            // Auto commit changes when limit is reached
            if (AutoCommit && _pendingDocuments[core].Count > AutoCommitCount)
            {
                Commit(scope);
            }
        }
예제 #3
0
        public void Execute()
        {
            Console.WriteLine("CLIENT SAMPLE");

            var client = new ElasticClient<Tweet>(defaultHost: "localhost", defaultPort: 9200);
            
            var firstTweet = new Tweet
            {
                User = "******",
                Message = "trying out Elastic Search"
            };

            var anotherTweet = new Tweet
            {
                User = "******",
                Message = "one more message"
            };


            var indexSettings = new IndexSettingsBuilder()
                    .NumberOfShards(8)
                    .NumberOfReplicas(1)
                    .Analysis(analysis => analysis
                        .Analyzer(analyzer => analyzer
                            .Custom("keyword_lowercase", custom => custom
                            .Tokenizer(DefaultTokenizers.keyword)
                            .Filter(DefaultTokenFilters.lowercase))));

            client.CreateIndex(new IndexCommand(index: "twitter").Refresh(), indexSettings);


            IndexResult indexResult1 = client.Index(new IndexCommand(index: "twitter", type: "tweet", id: "1").Refresh(),
                         firstTweet);

            IndexResult indexResult2 = client.Index(Commands.Index(index: "twitter", type: "tweet", id: "2").Refresh(),
                         anotherTweet);

            GetResult<Tweet> getResult = client.Get(new GetCommand(index: "twitter", type: "tweet", id: "2"));

            SearchResult<Tweet> searchResult = client.Search(new SearchCommand("twitter", "tweet"),
                                                            new QueryBuilder<Tweet>()
                                                            .Query(q => q
                                                                .Term(t => t
                                                                    .Field(tweet => tweet.User)
                                                                    .Value("testUser")
                                                                    .Boost(2)
                                                                )
                                                            ));

            DeleteResult deleteResult = client.Delete(Commands.Delete(index: "twitter"));


            PrintIndexResult(indexResult1);

            PrintIndexResult(indexResult2);

            PrintGetResult(getResult);

            PrintSearchResults(searchResult);

            PrintDeleteResult(deleteResult);

            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
예제 #4
0
        public void Execute()
        {
            Console.WriteLine("CLIENT SAMPLE");

            var client = new ElasticClient <Tweet>(defaultHost: "localhost", defaultPort: 9200);

            var firstTweet = new Tweet
            {
                User    = "******",
                Message = "trying out Elastic Search"
            };

            var anotherTweet = new Tweet
            {
                User    = "******",
                Message = "one more message"
            };


            var indexSettings = new IndexSettingsBuilder()
                                .NumberOfShards(8)
                                .NumberOfReplicas(1)
                                .Analysis(analysis => analysis
                                          .Analyzer(analyzer => analyzer
                                                    .Custom("keyword_lowercase", custom => custom
                                                            .Tokenizer(DefaultTokenizers.keyword)
                                                            .Filter(DefaultTokenFilters.lowercase))));

            client.CreateIndex(new IndexCommand(index: "twitter").Refresh(), indexSettings);


            IndexResult indexResult1 = client.Index(new IndexCommand(index: "twitter", type: "tweet", id: "1").Refresh(),
                                                    firstTweet);

            IndexResult indexResult2 = client.Index(Commands.Index(index: "twitter", type: "tweet", id: "2").Refresh(),
                                                    anotherTweet);

            GetResult <Tweet> getResult = client.Get(new GetCommand(index: "twitter", type: "tweet", id: "2"));

            SearchResult <Tweet> searchResult = client.Search(new SearchCommand("twitter", "tweet").TrackTotalHits(),
                                                              new QueryBuilder <Tweet>()
                                                              .Query(q => q
                                                                     .Term(t => t
                                                                           .Field(tweet => tweet.User)
                                                                           .Value("testUser")
                                                                           .Boost(2)
                                                                           )
                                                                     ));

            DeleteResult deleteResult = client.Delete(Commands.Delete(index: "twitter"));


            PrintIndexResult(indexResult1);

            PrintIndexResult(indexResult2);

            PrintGetResult(getResult);

            PrintSearchResults(searchResult);

            PrintDeleteResult(deleteResult);

            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
예제 #5
0
        public IndexResult CreateIndex(IndexCommand indexCommand, IndexSettingsBuilder indexSettings)
        {
            var result = connection.Put(indexCommand, indexSettings.Build());

            return(Serializer.ToIndexResult(result));
        }