コード例 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
										JsonSerializer serializer)
        {
            JObject o = JObject.Load(reader);
            var result = new IndexSettings();
            var dictionary = new Dictionary<string, object>();
            serializer.Populate(o.CreateReader(), dictionary);
            result.Settings = dictionary;
            result._ = DynamicDictionary.Create(dictionary);
            foreach (var rootProperty in o.Children<JProperty>())
            {
                if (rootProperty.Name.Equals("analysis", StringComparison.InvariantCultureIgnoreCase))
                {
                    result.Analysis = serializer.Deserialize<AnalysisSettings>(rootProperty.Value.CreateReader());
                    result.Settings.Remove(rootProperty.Name);
                }
                else if (rootProperty.Name.Equals("warmers", StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (var jWarmer in rootProperty.Value.Children<JProperty>())
                    {
                        result.Warmers[jWarmer.Name] = serializer.Deserialize<WarmerMapping>(jWarmer.Value.CreateReader());
                    }
                    result.Settings.Remove(rootProperty.Name);
                }
                else if (rootProperty.Name.Equals("similarity", StringComparison.InvariantCultureIgnoreCase))
                {
                    var baseSimilarity = ((JObject)rootProperty.Value).Property("base");
                    if (baseSimilarity != null)
                    {
                        baseSimilarity.Remove();
                        result.Similarity = new SimilaritySettings(((JObject)baseSimilarity.Value).Property("type").Value.ToString());
                    }
                    else
                    {
                        result.Similarity = new SimilaritySettings();
                    }

                    foreach (var similarityProperty in rootProperty.Value.Children<JProperty>())
                    {
                        var typeProperty = ((JObject)similarityProperty.Value).Property("type");
                        typeProperty.Remove();
                        var customSimilarity = new CustomSimilaritySettings(similarityProperty.Name, typeProperty.Value.ToString());
                        foreach (var similaritySetting in similarityProperty.Value.Children<JProperty>())
                        {
                            customSimilarity.SimilarityParameters.Add(similaritySetting.Name, similaritySetting.Value.ToString());
                        }

                        result.Similarity.CustomSimilarities.RemoveAll(x => x.Name == customSimilarity.Name);
                        result.Similarity.CustomSimilarities.Add(customSimilarity);
                    }
                    result.Settings.Remove(rootProperty.Name);
                }
            }
            return result;
        }
コード例 #2
0
ファイル: IndicesTests.cs プロジェクト: jayakrishnagopal/NEST
        public void GetIndexSettingsComplex()
        {
            var index = Guid.NewGuid().ToString();
            var settings = new IndexSettings();
            settings.NumberOfReplicas = 4;
            settings.NumberOfShards = 8;

            settings.Analysis.Analyzers.Add("snowball", new SnowballAnalyzer { Language = "English" });
            settings.Analysis.Analyzers.Add("standard", new StandardAnalyzer { StopWords = new[]{"word1", "word2"}});
            settings.Analysis.Analyzers.Add("swedishlanguage", new LanguageAnalyzer(Language.Swedish) { StopWords = new[] { "word1", "word2" }, StemExclusionList = new[] { "stem1", "stem2" } });

            settings.Analysis.CharFilters.Add("char1", new HtmlStripCharFilter());
            settings.Analysis.CharFilters.Add("char2", new MappingCharFilter{ Mappings = new []{"ph=>f", "qu=>q"}});

            settings.Analysis.TokenFilters.Add("tokenfilter1", new EdgeNGramTokenFilter());
            settings.Analysis.TokenFilters.Add("tokenfilter2", new SnowballTokenFilter());

            settings.Analysis.Tokenizers.Add("token1", new KeywordTokenizer());
            settings.Analysis.Tokenizers.Add("token2", new PathHierarchyTokenizer());

            settings.Similarity = new SimilaritySettings();
            var dfr = new CustomSimilaritySettings("test1", "DFR");
            dfr.SimilarityParameters.Add("basic_model", "g");
            dfr.SimilarityParameters.Add("after_effect", "l");
            dfr.SimilarityParameters.Add("normalization", "h2");
            dfr.SimilarityParameters.Add("normalization.h2.c", 3);
            settings.Similarity.CustomSimilarities.Add(dfr);

            var ib = new CustomSimilaritySettings("test2", "IB");
            ib.SimilarityParameters.Add("distribution", "spl");
            ib.SimilarityParameters.Add("lambda", "ttf");
            ib.SimilarityParameters.Add("normalization", "h1");
            settings.Similarity.CustomSimilarities.Add(ib);

            var typeMapping = this._client.GetMapping(ElasticsearchConfiguration.DefaultIndex, "elasticsearchprojects");
            typeMapping.TypeNameMarker = index;
            settings.Mappings.Add(typeMapping);

            settings.Add("merge.policy.merge_factor", "10");

            var createResponse = this._client.CreateIndex(index, settings);

            var r = this._client.GetIndexSettings(index);
            Assert.True(r.IsValid);
            Assert.NotNull(r.Settings);
            Assert.AreEqual(r.Settings.NumberOfReplicas, 4);
            Assert.AreEqual(r.Settings.NumberOfShards, 8);
            Assert.Greater(r.Settings.Count(), 0);
            Assert.True(r.Settings.ContainsKey("merge.policy.merge_factor"));
            Assert.AreEqual("10", r.Settings["merge.policy.merge_factor"]);

            Assert.AreEqual(3, r.Settings.Analysis.Analyzers.Count);
            { // assert analyzers
                Assert.True(r.Settings.Analysis.Analyzers.ContainsKey("snowball"));
                var snoballAnalyser = r.Settings.Analysis.Analyzers["snowball"] as SnowballAnalyzer;
                Assert.NotNull(snoballAnalyser);
                Assert.AreEqual("English", snoballAnalyser.Language);

                Assert.True(r.Settings.Analysis.Analyzers.ContainsKey("standard"));
                var standardAnalyser = r.Settings.Analysis.Analyzers["standard"] as StandardAnalyzer;
                Assert.NotNull(standardAnalyser);
                Assert.NotNull(standardAnalyser.StopWords);
                Assert.AreEqual(2, standardAnalyser.StopWords.Count());
                Assert.True(standardAnalyser.StopWords.Contains("word1"));
                Assert.True(standardAnalyser.StopWords.Contains("word2"));

                Assert.True(r.Settings.Analysis.Analyzers.ContainsKey("swedishlanguage"));
                var languageAnalyser = r.Settings.Analysis.Analyzers["swedishlanguage"] as LanguageAnalyzer;
                Assert.NotNull(languageAnalyser);
                Assert.AreEqual(Language.Swedish.ToString().ToLower(), languageAnalyser.Type);
                Assert.NotNull(languageAnalyser.StopWords);
                Assert.AreEqual(2, languageAnalyser.StopWords.Count());
                Assert.True(languageAnalyser.StopWords.Contains("word1"));
                Assert.True(languageAnalyser.StopWords.Contains("word2"));
                Assert.AreEqual(2, languageAnalyser.StemExclusionList.Count());
                Assert.True(languageAnalyser.StemExclusionList.Contains("stem1"));
                Assert.True(languageAnalyser.StemExclusionList.Contains("stem2"));
            }

            Assert.AreEqual(2, r.Settings.Analysis.CharFilters.Count);
            { // assert char filters
                Assert.True(r.Settings.Analysis.CharFilters.ContainsKey("char1"));
                var filter1 = r.Settings.Analysis.CharFilters["char1"] as HtmlStripCharFilter;
                Assert.NotNull(filter1);
                Assert.True(r.Settings.Analysis.CharFilters.ContainsKey("char2"));
                var filter2 = r.Settings.Analysis.CharFilters["char2"] as MappingCharFilter;
                Assert.NotNull(filter2);
                Assert.AreEqual(2, filter2.Mappings.Count());
                Assert.True(filter2.Mappings.Contains("ph=>f"));
                Assert.True(filter2.Mappings.Contains("qu=>q"));
            }

            Assert.AreEqual(2, r.Settings.Analysis.TokenFilters.Count);
            { // assert token filters
                Assert.True(r.Settings.Analysis.TokenFilters.ContainsKey("tokenfilter1"));
                var filter1 = r.Settings.Analysis.TokenFilters["tokenfilter1"] as EdgeNGramTokenFilter;
                Assert.NotNull(filter1);
                Assert.True(r.Settings.Analysis.TokenFilters.ContainsKey("tokenfilter2"));
                var filter2 = r.Settings.Analysis.TokenFilters["tokenfilter2"] as SnowballTokenFilter;
                Assert.NotNull(filter2);
            }

            Assert.AreEqual(2, r.Settings.Analysis.Tokenizers.Count);
            { // assert tokenizers
                Assert.True(r.Settings.Analysis.Tokenizers.ContainsKey("token1"));
                var tokenizer1 = r.Settings.Analysis.Tokenizers["token1"] as KeywordTokenizer;
                Assert.NotNull(tokenizer1);
                Assert.True(r.Settings.Analysis.Tokenizers.ContainsKey("token2"));
                var tokenizer2 = r.Settings.Analysis.Tokenizers["token2"] as PathHierarchyTokenizer;
                Assert.NotNull(tokenizer2);
            }

            Assert.NotNull(r.Settings.Similarity);
            Assert.NotNull(r.Settings.Similarity.CustomSimilarities);
            Assert.AreEqual(2, r.Settings.Similarity.CustomSimilarities.Count);
            { // assert similarity
                var similarity1 = r.Settings.Similarity.CustomSimilarities.FirstOrDefault(x => x.Name.Equals("test1", StringComparison.InvariantCultureIgnoreCase));
                Assert.NotNull(similarity1);
                Assert.AreEqual("DFR", similarity1.Type);
                Assert.AreEqual(4, similarity1.SimilarityParameters.Count);
                Assert.True(similarity1.SimilarityParameters.Any(x => x.Key.Equals("basic_model") && x.Value.ToString().Equals("g")));
                Assert.True(similarity1.SimilarityParameters.Any(x => x.Key.Equals("after_effect") && x.Value.ToString().Equals("l")));
                Assert.True(similarity1.SimilarityParameters.Any(x => x.Key.Equals("normalization") && x.Value.ToString().Equals("h2")));
                Assert.True(similarity1.SimilarityParameters.Any(x => x.Key.Equals("normalization.h2.c") && x.Value.ToString().Equals("3")));

                var similarity2 = r.Settings.Similarity.CustomSimilarities.FirstOrDefault(x => x.Name.Equals("test2", StringComparison.InvariantCultureIgnoreCase));
                Assert.NotNull(similarity2);
                Assert.AreEqual("IB", similarity2.Type);
                Assert.AreEqual(3, similarity2.SimilarityParameters.Count);
                Assert.True(similarity2.SimilarityParameters.Any(x => x.Key.Equals("distribution") && x.Value.ToString().Equals("spl")));
                Assert.True(similarity2.SimilarityParameters.Any(x => x.Key.Equals("lambda") && x.Value.ToString().Equals("ttf")));
                Assert.True(similarity2.SimilarityParameters.Any(x => x.Key.Equals("normalization") && x.Value.ToString().Equals("h1")));
            }
            this._client.DeleteIndex(index);
        }