Exemplo n.º 1
0
        internal static SearchIndex DeserializeSearchIndex(JsonElement element)
        {
            string name = default;
            IList <SearchField> fields = default;
            Optional <IList <ScoringProfile> > scoringProfiles = default;
            Optional <string>      defaultScoringProfile       = default;
            Optional <CorsOptions> corsOptions = default;
            Optional <IList <SearchSuggester> >    suggesters    = default;
            Optional <IList <LexicalAnalyzer> >    analyzers     = default;
            Optional <IList <LexicalTokenizer> >   tokenizers    = default;
            Optional <IList <TokenFilter> >        tokenFilters  = default;
            Optional <IList <CharFilter> >         charFilters   = default;
            Optional <IList <LexicalNormalizer> >  normalizers   = default;
            Optional <SearchResourceEncryptionKey> encryptionKey = default;
            Optional <SimilarityAlgorithm>         similarity    = default;
            Optional <SemanticSettings>            semantic      = default;
            Optional <string> odataEtag = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("fields"))
                {
                    List <SearchField> array = new List <SearchField>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(SearchField.DeserializeSearchField(item));
                    }
                    fields = array;
                    continue;
                }
                if (property.NameEquals("scoringProfiles"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <ScoringProfile> array = new List <ScoringProfile>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(ScoringProfile.DeserializeScoringProfile(item));
                    }
                    scoringProfiles = array;
                    continue;
                }
                if (property.NameEquals("defaultScoringProfile"))
                {
                    defaultScoringProfile = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("corsOptions"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        corsOptions = null;
                        continue;
                    }
                    corsOptions = CorsOptions.DeserializeCorsOptions(property.Value);
                    continue;
                }
                if (property.NameEquals("suggesters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <SearchSuggester> array = new List <SearchSuggester>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(SearchSuggester.DeserializeSearchSuggester(item));
                    }
                    suggesters = array;
                    continue;
                }
                if (property.NameEquals("analyzers"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <LexicalAnalyzer> array = new List <LexicalAnalyzer>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(LexicalAnalyzer.DeserializeLexicalAnalyzer(item));
                    }
                    analyzers = array;
                    continue;
                }
                if (property.NameEquals("tokenizers"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <LexicalTokenizer> array = new List <LexicalTokenizer>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(LexicalTokenizer.DeserializeLexicalTokenizer(item));
                    }
                    tokenizers = array;
                    continue;
                }
                if (property.NameEquals("tokenFilters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <TokenFilter> array = new List <TokenFilter>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(TokenFilter.DeserializeTokenFilter(item));
                    }
                    tokenFilters = array;
                    continue;
                }
                if (property.NameEquals("charFilters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <CharFilter> array = new List <CharFilter>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(CharFilter.DeserializeCharFilter(item));
                    }
                    charFilters = array;
                    continue;
                }
                if (property.NameEquals("normalizers"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <LexicalNormalizer> array = new List <LexicalNormalizer>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(LexicalNormalizer.DeserializeLexicalNormalizer(item));
                    }
                    normalizers = array;
                    continue;
                }
                if (property.NameEquals("encryptionKey"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        encryptionKey = null;
                        continue;
                    }
                    encryptionKey = SearchResourceEncryptionKey.DeserializeSearchResourceEncryptionKey(property.Value);
                    continue;
                }
                if (property.NameEquals("similarity"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    similarity = SimilarityAlgorithm.DeserializeSimilarityAlgorithm(property.Value);
                    continue;
                }
                if (property.NameEquals("semantic"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        semantic = null;
                        continue;
                    }
                    semantic = SemanticSettings.DeserializeSemanticSettings(property.Value);
                    continue;
                }
                if (property.NameEquals("@odata.etag"))
                {
                    odataEtag = property.Value.GetString();
                    continue;
                }
            }
            return(new SearchIndex(name, fields, Optional.ToList(scoringProfiles), defaultScoringProfile.Value, corsOptions.Value, Optional.ToList(suggesters), Optional.ToList(analyzers), Optional.ToList(tokenizers), Optional.ToList(tokenFilters), Optional.ToList(charFilters), Optional.ToList(normalizers), encryptionKey.Value, similarity.Value, semantic.Value, odataEtag.Value));
        }
 /// <summary>
 /// Persists class-specific properties into the given <see cref="SearchField"/>.
 /// </summary>
 /// <param name="field">The <see cref="SearchField"/> into which properties are persisted.</param>
 private protected abstract void Save(SearchField field);
Exemplo n.º 3
0
        public static SearchField ToSearchField(this Azure.Search.Documents.Indexes.Models.SearchField field)
        {
            Type type;

            if (field.Type == SearchFieldDataType.Boolean)
            {
                type = typeof(Boolean);
            }
            else if (field.Type == SearchFieldDataType.DateTimeOffset)
            {
                type = typeof(DateTime);
            }
            else if (field.Type == SearchFieldDataType.Double)
            {
                type = typeof(double);
            }
            else if (field.Type == SearchFieldDataType.Int32)
            {
                type = typeof(Int32);
            }
            else if (field.Type == SearchFieldDataType.Int64)
            {
                type = typeof(Int64);
            }
            else if (field.Type == SearchFieldDataType.String)
            {
                type = typeof(string);
            }
            else if (field.Type == SearchFieldDataType.GeographyPoint)
            {
                type = typeof(GeographyPoint);
            }

            // Azure Search SearchFieldDataType objects don't follow value comparisons, so use overloaded string conversion operator to be a consistent representation
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.String).ToString())
            {
                type = typeof(string[]);
            }
            else if (field.Type == SearchFieldDataType.Complex)
            {
                type = typeof(string);
            }
            else if (field.Type == SearchFieldDataType.Collection(SearchFieldDataType.Complex))
            {
                type = typeof(string[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.DateTimeOffset).ToString())
            {
                type = typeof(DateTime[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.GeographyPoint).ToString())
            {
                type = typeof(Microsoft.Spatial.GeographyPoint[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.Double).ToString())
            {
                type = typeof(double[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.Boolean).ToString())
            {
                type = typeof(Boolean[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.Int64).ToString())
            {
                type = typeof(Int32[]);
            }
            else if (field.Type.ToString() == SearchFieldDataType.Collection(SearchFieldDataType.Int64).ToString())
            {
                type = typeof(Int64[]);
            }
            else
            {
                throw new ArgumentException($"Cannot map {field.Type} to a C# type");
            }
            return(new SearchField()
            {
                Name = field.Name,
                Type = type,
                IsFacetable = field.IsFacetable ?? false,
                IsFilterable = field.IsFilterable ?? false,
                IsKey = field.IsKey ?? false,
                IsHidden = field.IsHidden ?? false,
                IsSearchable = field.IsSearchable ?? false,
                IsSortable = field.IsSortable ?? false
            });
        }
        internal static SearchIndex DeserializeSearchIndex(JsonElement element)
        {
            string name = default;
            IList <SearchField>    fields                     = default;
            IList <ScoringProfile> scoringProfiles            = default;
            string                      defaultScoringProfile = default;
            CorsOptions                 corsOptions           = default;
            IList <SearchSuggester>     suggesters            = default;
            IList <LexicalAnalyzer>     analyzers             = default;
            IList <LexicalTokenizer>    tokenizers            = default;
            IList <TokenFilter>         tokenFilters          = default;
            IList <CharFilter>          charFilters           = default;
            SearchResourceEncryptionKey encryptionKey         = default;
            SimilarityAlgorithm         similarity            = default;
            string                      odataEtag             = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("name"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("fields"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <SearchField> array = new List <SearchField>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(SearchField.DeserializeSearchField(item));
                        }
                    }
                    fields = array;
                    continue;
                }
                if (property.NameEquals("scoringProfiles"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <ScoringProfile> array = new List <ScoringProfile>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(ScoringProfile.DeserializeScoringProfile(item));
                        }
                    }
                    scoringProfiles = array;
                    continue;
                }
                if (property.NameEquals("defaultScoringProfile"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    defaultScoringProfile = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("corsOptions"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    corsOptions = CorsOptions.DeserializeCorsOptions(property.Value);
                    continue;
                }
                if (property.NameEquals("suggesters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <SearchSuggester> array = new List <SearchSuggester>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(SearchSuggester.DeserializeSearchSuggester(item));
                        }
                    }
                    suggesters = array;
                    continue;
                }
                if (property.NameEquals("analyzers"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <LexicalAnalyzer> array = new List <LexicalAnalyzer>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(LexicalAnalyzer.DeserializeLexicalAnalyzer(item));
                        }
                    }
                    analyzers = array;
                    continue;
                }
                if (property.NameEquals("tokenizers"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <LexicalTokenizer> array = new List <LexicalTokenizer>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(LexicalTokenizer.DeserializeLexicalTokenizer(item));
                        }
                    }
                    tokenizers = array;
                    continue;
                }
                if (property.NameEquals("tokenFilters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <TokenFilter> array = new List <TokenFilter>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(TokenFilter.DeserializeTokenFilter(item));
                        }
                    }
                    tokenFilters = array;
                    continue;
                }
                if (property.NameEquals("charFilters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <CharFilter> array = new List <CharFilter>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(CharFilter.DeserializeCharFilter(item));
                        }
                    }
                    charFilters = array;
                    continue;
                }
                if (property.NameEquals("encryptionKey"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    encryptionKey = SearchResourceEncryptionKey.DeserializeSearchResourceEncryptionKey(property.Value);
                    continue;
                }
                if (property.NameEquals("similarity"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    similarity = SimilarityAlgorithm.DeserializeSimilarityAlgorithm(property.Value);
                    continue;
                }
                if (property.NameEquals("@odata.etag"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    odataEtag = property.Value.GetString();
                    continue;
                }
            }
            return(new SearchIndex(name, fields, scoringProfiles, defaultScoringProfile, corsOptions, suggesters, analyzers, tokenizers, tokenFilters, charFilters, encryptionKey, similarity, odataEtag));
        }