Пример #1
0
        public static SearchField CreateSearchField(JToken field)
        {
            SearchField sField = new SearchableField((string)field["name"], field.GetType().IsArray);

            if (field["facetable"] != null)
            {
                sField.IsFacetable = (bool)field["facetable"];
            }
            if (field["filterable"] != null)
            {
                sField.IsFilterable = (bool)field["filterable"];
            }
            if (field["retrievable"] != null)
            {
                sField.IsHidden = !(bool)field["retrievable"];
            }
            if (field["sortable"] != null)
            {
                sField.IsSortable = (bool)field["sortable"];
            }
            if (field["key"] != null)
            {
                sField.IsKey = (bool)field["key"];
            }
            if (field["searchable"] != null)
            {
                sField.IsSearchable = (bool)field["searchable"];
            }
            // sField.SearchAnalyzerName =  field["searchAnalyzer"] == null ? "" : (string)field["searchAnalyzer"];
            // sField.IndexAnalyzerName = field["indexAnalyzer"] == null ? "" : (string)field["indexAnalyzer"];
            // sField.AnalyzerName = (string)field["analyzer"];

            return(sField);
        }
        /// <inheritdoc/>
        SearchField ISearchFieldAttribute.CreateField(string name)
        {
            SearchableField field = new SearchableField(name, Type.IsCollection)
            {
                IsKey           = IsKey,
                IsHidden        = IsHidden,
                IsFilterable    = IsFilterable,
                IsFacetable     = IsFacetable,
                IsSortable      = IsSortable,
                SynonymMapNames = SynonymMapNames,
            };

            if (AnalyzerName != null)
            {
                field.AnalyzerName = AnalyzerName;
            }

            if (SearchAnalyzerName != null)
            {
                field.SearchAnalyzerName = SearchAnalyzerName;
            }

            if (IndexAnalyzerName != null)
            {
                field.IndexAnalyzerName = IndexAnalyzerName;
            }

            return(field);
        }
        public SearchField GetSearchField(MemberInfo member)
        {
            var memberType = member.GetPropertyOrFieldType();

            if (typeof(int).IsAssignableFrom(memberType))
            {
                var field = new SimpleField(this.Name, SearchFieldDataType.Int32);
                return(PopulateFieldKey(field));
            }
            if (typeof(DateTime).IsAssignableFrom(memberType))
            {
                var field = new SimpleField(this.Name, SearchFieldDataType.DateTimeOffset);
                return(PopulateFieldKey(field));
            }
            var searchableField = new SearchableField("hotelName");

            return(PopulateFieldKey(searchableField));
        }
        public async Task CreateIndex()
        {
            await using SearchResources resources = SearchResources.CreateWithNoIndexes(this);
            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex
            Uri    endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            string key      = Environment.GetEnvironmentVariable("SEARCH_API_KEY");

            // Define client options to use camelCase when serializing property names.
            SearchClientOptions options = new SearchClientOptions(ServiceVersion)
            {
                Serializer = new JsonObjectSerializer(
                    new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                })
            };

            // Create a service client.
            AzureKeyCredential credential  = new AzureKeyCredential(key);
            SearchIndexClient  indexClient = new SearchIndexClient(endpoint, credential, options);
#if !SNIPPET
            indexClient = resources.GetIndexClient(options);
#endif

            // Create the FieldBuilder using the same serializer.
            FieldBuilder fieldBuilder = new FieldBuilder
            {
                Serializer = options.Serializer
            };

            // Create the index using FieldBuilder.
#if SNIPPET
            SearchIndex index = new SearchIndex("movies")
#else
            SearchIndex index = new SearchIndex(Recording.Random.GetName())
#endif
            {
                Fields     = fieldBuilder.Build(typeof(Movie)),
                Suggesters =
                {
                    // Suggest query terms from the "name" field.
                    new SearchSuggester("n", "name")
                }
            };

            // Define the "genre" field as a string.
            SearchableField genreField = new SearchableField("genre")
            {
                AnalyzerName = LexicalAnalyzerName.Values.EnLucene,
                IsFacetable  = true,
                IsFilterable = true
            };
            index.Fields.Add(genreField);

            // Create the index.
            indexClient.CreateIndex(index);
            #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_CreateIndex

            // Make sure the index is removed.
            resources.IndexName = index.Name;

            #region Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument
            Movie movie = new Movie
            {
                Id     = Guid.NewGuid().ToString("n"),
                Name   = "The Lord of the Rings: The Return of the King",
                Genre  = MovieGenre.Fantasy,
                Year   = 2003,
                Rating = 9.1
            };

            // Add a movie to the index.
            SearchClient searchClient = indexClient.GetSearchClient(index.Name);
            searchClient.UploadDocuments(new[] { movie });
            #endregion Snippet:Azure_Search_Tests_Sample2_FieldBuilderIgnore_UploadDocument
        }