public AzureField(string name, object value, Field field, Microsoft.Azure.Search.Models.DataType dataType = null)
 {
     Name = name;
     Value = value;
     Field = field;
     DataType = dataType ?? Microsoft.Azure.Search.Models.DataType.String;
 }
        private bool TryGetDataType(Type type, out DataType dt)
        {
            if (type == typeof(string))
            {
                dt = DataType.String;
                return(true);
            }

            if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
            {
                dt = DataType.DateTimeOffset;
                return(true);
            }

            if (type == typeof(bool))
            {
                dt = DataType.Boolean;
                return(true);
            }

            if (type == typeof(int))
            {
                dt = DataType.Int32;
                return(true);
            }

            if (type == typeof(long))
            {
                dt = DataType.Int64;
                return(true);
            }

            if (type == typeof(decimal))
            {
                dt = DataType.Double;
                return(true);
            }

            if (type == typeof(double))
            {
                dt = DataType.Double;
                return(true);
            }

            if (type == typeof(float))
            {
                dt = DataType.Double;
                return(true);
            }

            if (type == typeof(Guid))
            {
                dt = DataType.String;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a DataType for a collection of the given type.
        /// </summary>
        /// <param name="elementType">The DataType of the elements of the collection.</param>
        /// <returns>A new DataType for a collection.</returns>
        public static DataType Collection(DataType elementType)
        {
            if (elementType == null)
            {
                throw new ArgumentNullException("elementType");
            }

            return new DataType(System.String.Format("Collection({0})", elementType.ToString()));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Get a Search Index for the Hotels sample data.
 ///
 /// This index is tuned more for exercising document serialization,
 /// indexing, and querying operations. Also, the fields of this index
 /// should exactly match the properties of the Hotel test model class
 /// below.
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 private static Microsoft.Azure.Search.Models.Index GetHotelIndex(string name) =>
 new Microsoft.Azure.Search.Models.Index()
 {
     Name   = name,
     Fields = new[]
     {
         Field.New("hotelId", DataType.String, isKey: true, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("hotelName", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: false),
         Field.NewSearchableString("description", AnalyzerName.EnLucene),
         Field.NewSearchableString("descriptionFr", AnalyzerName.FrLucene),
         Field.New("category", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("tags", DataType.Collection(DataType.String), isSearchable: true, isFilterable: true, isFacetable: true),
         Field.New("parkingIncluded", DataType.Boolean, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("smokingAllowed", DataType.Boolean, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("lastRenovationDate", DataType.DateTimeOffset, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("rating", DataType.Int32, isFilterable: true, isSortable: true, isFacetable: true),
         Field.New("location", DataType.GeographyPoint, isFilterable: true, isSortable: true),
         Field.NewComplex("address", isCollection: false, fields: new[]
         {
             Field.New("streetAddress", DataType.String, isSearchable: true),
             Field.New("city", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: true),
             Field.New("stateProvince", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: true),
             Field.New("country", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: true),
             Field.New("postalCode", DataType.String, isSearchable: true, isFilterable: true, isSortable: true, isFacetable: true)
         }),
         Field.NewComplex("rooms", isCollection: true, fields: new[]
         {
             Field.NewSearchableString("description", AnalyzerName.EnLucene),
             Field.NewSearchableString("descriptionFr", AnalyzerName.FrLucene),
             Field.New("type", DataType.String, isSearchable: true, isFilterable: true, isFacetable: true),
             Field.New("baseRate", DataType.Double, isFilterable: true, isFacetable: true),
             Field.New("bedOptions", DataType.String, isSearchable: true, isFilterable: true, isFacetable: true),
             Field.New("sleepsCount", DataType.Int32, isFilterable: true, isFacetable: true),
             Field.New("smokingAllowed", DataType.Boolean, isFilterable: true, isFacetable: true),
             Field.New("tags", DataType.Collection(DataType.String), isSearchable: true, isFilterable: true, isFacetable: true)
         })
     },
     Suggesters = new[]
     {
         new Suggester(
             name: "sg",
             sourceFields: new[] { "description", "hotelName" })
     },
     ScoringProfiles = new[]
     {
         new ScoringProfile("nearest")
         {
             FunctionAggregation = ScoringFunctionAggregation.Sum,
             Functions           = new[]
             {
                 new DistanceScoringFunction("location", 2, new DistanceScoringParameters("myloc", 100))
             }
         }
     }
 };
        /// <summary>
        /// Initializes a new instance of the Field class with required arguments.
        /// </summary>
        /// <param name="name">The name of the field.</param>
        /// <param name="dataType">The data type of the field.</param>
        /// <param name="analyzerName">The name of the analyzer to use for the field.</param>
        /// <remarks>The new field will automatically be searchable.</remarks>
        public Field(string name, DataType dataType, AnalyzerName analyzerName)
            : this(name, dataType != null ? dataType.ToString() : null)
        {
            if (analyzerName == null)
            {
                throw new ArgumentNullException("analyzerName");
            }

            Analyzer = analyzerName.ToString();
            IsSearchable = true;
        }
        private List <Field> GetTypeFields(Type sourceType, bool isTop)
        {
            var source = TypeAccessor.Create(sourceType);

            return(source.GetMembers().Select(x =>
            {
                if (x.GetAttribute(typeof(JsonIgnoreAttribute), false) != null)
                {
                    return null;
                }

                if (x.IsList())
                {
                    var argType = x.Type.GetGenericArguments()[0];
                    return new Field(x.Name, DataType.Collection(DataType.Complex), GetTypeFields(argType, false));
                }

                DataType type;

                bool hasType = true;
                if (!TryGetDataType(x.Type, out type))
                {
                    if (x.Type.IsClass)
                    {
                        return new Field(x.Name, DataType.Complex, GetTypeFields(x.Type, false));
                    }

                    hasType = false;
                }

                if (!hasType)
                {
                    throw new ArgumentException($"Type of {x.Type.FullName} cannot be translated to Search Data-type.");
                }

                var isSearchable = x.Type == typeof(string);

                var isKey = isTop && x.GetAttribute(typeof(KeyAttribute), false) != null;
                var isFacetable = x.GetAttribute(typeof(IsFacetableAttribute), false) != null;
                var isFilterable = x.GetAttribute(typeof(IsFilterableAttribute), false) != null;

                var field = new Field(x.Name, type)
                {
                    IsKey = isKey,
                    IsSearchable = !isKey && isSearchable,
                    IsFacetable = isFacetable,
                    IsFilterable = isFilterable
                };

                return field;
            }).Where(field => field != null).ToList());
        }
Ejemplo n.º 7
0
        protected virtual Field CreateProviderField(string documentType, string fieldName, IndexDocumentField field)
        {
            DataType providerFieldType;

            if (field.ValueType == IndexDocumentFieldValueType.Undefined)
            {
                var originalFieldType = field.Value?.GetType() ?? typeof(object);
                providerFieldType = GetProviderFieldType(documentType, fieldName, originalFieldType);
            }
            else
            {
                providerFieldType = GetProviderFieldType(documentType, fieldName, field.ValueType);
            }

            var isGeoPoint   = providerFieldType == DataType.GeographyPoint;
            var isString     = providerFieldType == DataType.String;
            var isCollection = field.IsCollection && isString;

            if (isCollection)
            {
                providerFieldType = DataType.Collection(providerFieldType);
            }

            var providerField = new Field(fieldName, providerFieldType)
            {
                IsKey         = fieldName == AzureSearchHelper.KeyFieldName,
                IsRetrievable = field.IsRetrievable,
                IsSearchable  = isString && field.IsSearchable,
                IsFilterable  = field.IsFilterable,
                IsFacetable   = field.IsFilterable && !isGeoPoint,
                IsSortable    = field.IsFilterable && !isCollection,
            };

            if (providerField.IsSearchable == true)
            {
                providerField.IndexAnalyzer  = ContentAnalyzerName;
                providerField.SearchAnalyzer = AnalyzerName.StandardLucene;
            }

            return(providerField);
        }
 public void SetValues()
 {
     foreach (var keyValuePair in base.Attributes)
     {
         switch (keyValuePair.Key)
         {
             case "IsRetrievable":
                 this.IsRetrievable = GetBool(keyValuePair.Value, true);
                 break;
             case "IsFacetable":
                 this.IsFacetable = GetBool(keyValuePair.Value);
                 break;
             case "IsSortable":
                 IsSortable = GetBool(keyValuePair.Value);
                 break;
             case "IsFilterable":
                 IsFilterable = GetBool(keyValuePair.Value);
                 break;
             case "IsSearchable":
                 IsSearchable = GetBool(keyValuePair.Value, true);
                 break;
             case "IsKey":
                 IsKey = GetBool(keyValuePair.Value);
                 break;
             case "type":
                 Type = GetType(keyValuePair.Value);
                 break;
             case "nullValue":
                 SetNullPlaceholderValue(keyValuePair.Value);
                 break;
             case "emptyString":
                 SetEmptyStringPlaceholderValue(keyValuePair.Value);
                 break;
             case "boost":
                 Boost = ParseBoost(keyValuePair.Value);
                 break;
             default:
                 break;
         }
     }
 }
 public void SetType(string type)
 {
     Type = GetType(type);
 }
 /// <summary>
 /// Initializes a new instance of the Field class with required arguments.
 /// </summary>
 /// <param name="name">The name of the field.</param>
 /// <param name="dataType">The data type of the field.</param>
 public Field(string name, DataType dataType) : this(name, dataType != null ? dataType.ToString() : null)
 {
     // Do nothing.
 }
Ejemplo n.º 11
0
        public static DataAccessResponseType AddProductPropertyToSearchIndexFields(string searchPartition, string indexName, string fieldName, string propertyTypeNameKey) //, string searchPartitionName, string searchPartitionKey)
        {
            var searchUpdateResponse = new DataAccessResponseType();

            try
            {
                #region Determine Search Field DataType from PropertyType

                //Default is string:
                Microsoft.Azure.Search.Models.DataType fieldDataType = Microsoft.Azure.Search.Models.DataType.String;

                switch (propertyTypeNameKey)
                {
                case "number":
                    fieldDataType = Microsoft.Azure.Search.Models.DataType.Double;
                    break;

                case "datetime":
                    fieldDataType = Microsoft.Azure.Search.Models.DataType.DateTimeOffset;
                    break;

                case "location":
                    fieldDataType = Microsoft.Azure.Search.Models.DataType.GeographyPoint;
                    break;

                case "predefined":
                    fieldDataType = Microsoft.Azure.Search.Models.DataType.Collection(Microsoft.Azure.Search.Models.DataType.String);
                    break;

                case "swatch":
                    fieldDataType = Microsoft.Azure.Search.Models.DataType.Collection(Microsoft.Azure.Search.Models.DataType.String);
                    break;

                default:
                    break;
                }

                #endregion

                //SearchServiceClient searchServiceClient = Settings.Azure.Search.AccountsSearchServiceClient;
                //SearchServiceClient searchServiceClient = new SearchServiceClient(searchPartitionName, new SearchCredentials(searchPartitionKey));

                //Get search partition
                SearchServiceClient searchServiceClient = Settings.Azure.Search.GetSearchPartitionClient(searchPartition);

                //Get Index -----------------------------------
                Microsoft.Azure.Search.Models.Index index = searchServiceClient.Indexes.Get(indexName);

                //Manage field options based on DataType

                bool isSearchable  = false;
                bool isSortable    = true;
                bool isFacetable   = true;
                bool isRetrievable = true;
                bool isFilterable  = true;

                if (propertyTypeNameKey == "paragraph")
                {
                    isSearchable = true;  //<-- Only Strings and Collections of Strngs can be searchable
                    isFacetable  = false; //<-- Paragraphs SHOULD not be set as facetable in Azure search
                    isSortable   = false; //<-- Paragraphs SHOULD not be set as sortable in Azure search
                    //isRetrievable = false;  //<-- Paragraphs SHOULD not be returned in results (Now we do return paragraphs since we use this for detail pages)
                    isFilterable = false; //<-- Paragraphs SHOULD not be filterable
                }
                else if (propertyTypeNameKey == "string")
                {
                    isFacetable  = false; //<-- Allows for string searches of ID's, etc. + Saves storage on search index on fre form strings
                    isSearchable = true;
                }
                else if (propertyTypeNameKey == "predefined" || propertyTypeNameKey == "swatch")
                {
                    isSearchable = true;  //<-- Collections CAN be searchable
                    isSortable   = false; //<-- Collections CAN NOT be set as sortable in Azure search
                }
                else if (propertyTypeNameKey == "location")
                {
                    isFacetable = false; //<-- Geography points CAN NOT be set as facetable in Azure search
                }

                index.Fields.Add(new Field {
                    Name = fieldName, Type = fieldDataType, IsFilterable = isFilterable, IsRetrievable = isRetrievable, IsSearchable = isSearchable, IsFacetable = isFacetable, IsSortable = isSortable
                });

                #region Add an additional string metadata field for geography data

                //Allows us to search for address copy in order to get back products with the associated geographic points
                //We append the term "LocationMetadata" to the field name and create a SEARCHABLE ONLY field
                //We now added ability to retrieve so it can be unpacked by API calls and merged with location results

                if (propertyTypeNameKey == "location")
                {
                    index.Fields.Add(new Field {
                        Name = fieldName + "LocationMetadata", Type = Microsoft.Azure.Search.Models.DataType.String, IsFilterable = false, IsRetrievable = true, IsSearchable = true, IsFacetable = false, IsSortable = false
                    });
                }

                #endregion

                var indexResult = searchServiceClient.Indexes.CreateOrUpdate(index);

                if (indexResult != null)
                {
                    searchUpdateResponse.isSuccess = true;
                }
            }
            catch (Exception e)
            {
                searchUpdateResponse.isSuccess    = false;
                searchUpdateResponse.ErrorMessage = e.Message;

                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to create new field '" + fieldName + "' for index '" + indexName + "'",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                /*
                 * PlatformLogManager.LogActivity(
                 *          CategoryType.ManualTask,
                 *          ActivityType.ManualTask_Search,
                 *          "Search index field creation failed during setup of new field '" + fieldName + "' on index '" + indexName + "'",
                 *          "You may need to manually create index field '" + fieldName + "' on index '" + indexName + "'",
                 *          null,
                 *          null,
                 *          null,
                 *          null,
                 *          null,
                 *          null,
                 *          System.Reflection.MethodBase.GetCurrentMethod().ToString()
                 *      );*/
            }

            return(searchUpdateResponse);
        }
Ejemplo n.º 12
0
        private List <Field> GetTypeFields(Type sourceType)
        {
            var source = TypeAccessor.Create(sourceType);

            return(source.GetMembers().Select(x =>
            {
                if (x.GetAttribute(typeof(JsonIgnoreAttribute), false) != null)
                {
                    return null;
                }

                DataType type = null;
                var isSearchable = false;

                if (x.Type == typeof(string))
                {
                    type = DataType.String;
                    isSearchable = true;
                }

                if (x.Type == typeof(DateTime))
                {
                    type = DataType.DateTimeOffset;
                }

                if (x.Type == typeof(bool))
                {
                    type = DataType.Boolean;
                }

                if (x.Type == typeof(int))
                {
                    type = DataType.Int32;
                }

                if (x.Type == typeof(long))
                {
                    type = DataType.Int64;
                }

                if (x.Type == typeof(decimal))
                {
                    type = DataType.Double;
                }

                if (x.Type == typeof(double))
                {
                    type = DataType.Double;
                }

                if (x.Type == typeof(float))
                {
                    type = DataType.Double;
                }

                if (x.Type == typeof(Guid))
                {
                    type = DataType.String;
                }

                if (type == null)
                {
                    throw new ArgumentException($"Type of {x.Type.FullName} cannot be translated to Search Data-type.");
                }

                var isKey = x.GetAttribute(typeof(KeyAttribute), false) != null;

                var field = new Field(x.Name, type)
                {
                    IsKey = isKey,
                    IsSearchable = !isKey && isSearchable
                };

                return field;
            }).Where(field => field != null).ToList());
        }