Пример #1
0
        public void CanSearchWithCustomAnalyzer()
        {
            Run(() =>
            {
                const string CustomAnalyzerName   = "my_email_analyzer";
                const string CustomCharFilterName = "my_email_filter";

                Index index = new Index()
                {
                    Name   = SearchTestUtilities.GenerateName(),
                    Fields = new[]
                    {
                        new Field("id", DataType.String)
                        {
                            IsKey = true
                        },
                        new Field("message", AnalyzerName.Create(CustomAnalyzerName))
                        {
                            IsSearchable = true
                        }
                    },
                    Analyzers = new[]
                    {
                        new CustomAnalyzer()
                        {
                            Name        = CustomAnalyzerName,
                            Tokenizer   = TokenizerName.Standard,
                            CharFilters = new[] { CharFilterName.Create(CustomCharFilterName) }
                        }
                    },
                    CharFilters = new[] { new PatternReplaceCharFilter(CustomCharFilterName, "@", "_") }
                };

                Data.GetSearchServiceClient().Indexes.Create(index);

                SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

                var documents = new[]
                {
                    new Document()
                    {
                        { "id", "1" }, { "message", "My email is [email protected]." }
                    },
                    new Document()
                    {
                        { "id", "2" }, { "message", "His email is [email protected]." }
                    },
                };

                indexClient.Documents.Index(IndexBatch.Upload(documents));
                SearchTestUtilities.WaitForIndexing();

                DocumentSearchResult result = indexClient.Documents.Search("*****@*****.**");

                Assert.Equal("1", result.Results.Single().Document["id"]);
            });
        }
        public static Field ToAzureField(this SearchField field)
        {
            var t = DataType.String;

            switch (field.Type.ToLower())
            {
            case "bool":
                t = DataType.Boolean;
                break;

            case "int":
                t = DataType.Int32;
                break;

            case "collection":
                t = DataType.Collection(DataType.String);
                break;

            case "date":
                t = DataType.DateTimeOffset;
                break;
            }

            var f = new Field()
            {
                Name         = field.Name,
                Type         = t,
                IsFacetable  = field.IsFacetable,
                IsFilterable = field.IsFilterable,
                IsSortable   = field.IsSortable,
                IsSearchable = field.IsSearchable,
                IsKey        = field.IsKey,
                Analyzer     = !field.Analyzer.IsNullOrWhiteSpace() ? AnalyzerName.Create(field.Analyzer) : null
            };

            return(f);
        }
Пример #3
0
        /// <summary>
        /// Creates a collection of <see cref="Field"/> objects corresponding to
        /// the properties of the type supplied.
        /// </summary>
        /// <typeparam name="T">
        /// The type for which fields will be created, based on its properties.
        /// </typeparam>
        /// <param name="contractResolver">
        /// Contract resolver that the SearchIndexClient will use.
        /// This ensures that the field names are generated in a way that is
        /// consistent with the way the model will be serialized.
        /// </param>
        /// <returns>A collection of fields.</returns>
        public static IList <Field> BuildForType <T>(IContractResolver contractResolver)
        {
            var contract = (JsonObjectContract)contractResolver.ResolveContract(typeof(T));
            var fields   = new List <Field>();

            foreach (JsonProperty prop in contract.Properties)
            {
                IList <Attribute> attributes = prop.AttributeProvider.GetAttributes(true);
                if (attributes.Any(attr => attr is JsonIgnoreAttribute))
                {
                    continue;
                }

                DataType dataType = GetDataType(prop.PropertyType, prop.PropertyName);

                var field = new Field(prop.PropertyName, dataType);

                foreach (Attribute attribute in attributes)
                {
                    IsRetrievableAttribute  isRetrievableAttribute;
                    AnalyzerAttribute       analyzerAttribute;
                    SearchAnalyzerAttribute searchAnalyzerAttribute;
                    IndexAnalyzerAttribute  indexAnalyzerAttribute;
                    if (attribute is IsSearchableAttribute)
                    {
                        field.IsSearchable = true;
                    }
                    else if (attribute is IsFilterableAttribute)
                    {
                        field.IsFilterable = true;
                    }
                    else if (attribute is IsSortableAttribute)
                    {
                        field.IsSortable = true;
                    }
                    else if (attribute is IsFacetableAttribute)
                    {
                        field.IsFacetable = true;
                    }
                    else if ((isRetrievableAttribute = attribute as IsRetrievableAttribute) != null)
                    {
                        field.IsRetrievable = isRetrievableAttribute.IsRetrievable;
                    }
                    else if ((analyzerAttribute = attribute as AnalyzerAttribute) != null)
                    {
                        field.Analyzer = AnalyzerName.Create(analyzerAttribute.Name);
                    }
                    else if ((searchAnalyzerAttribute = attribute as SearchAnalyzerAttribute) != null)
                    {
                        field.SearchAnalyzer = AnalyzerName.Create(searchAnalyzerAttribute.Name);
                    }
                    else if ((indexAnalyzerAttribute = attribute as IndexAnalyzerAttribute) != null)
                    {
                        field.IndexAnalyzer = AnalyzerName.Create(indexAnalyzerAttribute.Name);
                    }
                    else
                    {
                        // Match on name to avoid dependency - don't want to force people not using
                        // this feature to bring in the annotations component.
                        Type attributeType = attribute.GetType();
                        if (attributeType.FullName == "System.ComponentModel.DataAnnotations.KeyAttribute")
                        {
                            field.IsKey = true;
                        }
                    }
                }

                fields.Add(field);
            }

            return(fields);
        }
Пример #4
0
 private static Index IndexDefinition()
 {
     return(new Index()
     {
         Name = "types",
         Fields = new[]
         {
             new Field("id", DataType.String)
             {
                 IsKey = true, IsRetrievable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsSearchable = false
             },
             new Field("namespaces", DataType.Collection(DataType.String))
             {
                 IsRetrievable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsSearchable = true, IndexAnalyzer = AnalyzerName.Create("name_index"), SearchAnalyzer = AnalyzerName.Create("name_search")
             },
             new Field("types", DataType.Collection(DataType.String))
             {
                 IsRetrievable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsSearchable = true, IndexAnalyzer = AnalyzerName.Create("name_index"), SearchAnalyzer = AnalyzerName.Create("name_search")
             },
             new Field("typesCamelHump", DataType.Collection(DataType.String))
             {
                 IsRetrievable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsSearchable = true, IndexAnalyzer = AnalyzerName.Create("camel_hump_index"), SearchAnalyzer = AnalyzerName.Create("camel_hump_search")
             },
             new Field("packageId", DataType.String)
             {
                 IsRetrievable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsSearchable = false
             },
             new Field("packageVersion", DataType.String)
             {
                 IsRetrievable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsSearchable = false
             },
             new Field("prerelease", DataType.Boolean)
             {
                 IsRetrievable = false, IsFilterable = true, IsSortable = false, IsFacetable = true, IsSearchable = false
             },
             new Field("netstandardVersion", DataType.Int32)
             {
                 IsRetrievable = true, IsFilterable = true, IsSortable = false, IsFacetable = true, IsSearchable = false
             },
             new Field("published", DataType.DateTimeOffset)
             {
                 IsRetrievable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsSearchable = false
             },
             new Field("totalDownloadCount", DataType.Int32)
             {
                 IsRetrievable = true, IsFilterable = true, IsSortable = true, IsFacetable = false, IsSearchable = false
             },
         },
         CorsOptions = new CorsOptions()
         {
             AllowedOrigins = new[] { "*" },
             MaxAgeInSeconds = (long)TimeSpan.FromHours(2).TotalSeconds,
         },
         CharFilters = new CharFilter[]
         {
             new PatternReplaceCharFilter
             {
                 Name = "remove_generics",
                 Pattern = "<[^>]*>",
                 Replacement = ".",
             },
             new PatternReplaceCharFilter
             {
                 Name = "remove_non_uppercase",
                 Pattern = "[^A-Z]+",
                 Replacement = ".",
             },
             new MappingCharFilter
             {
                 Name = "period_to_space",
                 Mappings = new[] { @".=>\u0020" },
             },
             new MappingCharFilter
             {
                 Name = "period_to_empty_string",
                 Mappings = new[] { @".=>" },
             },
         },
         TokenFilters = new TokenFilter[]
         {
             new EdgeNGramTokenFilter
             {
                 Name = "my_ngram",
                 MinGram = 2,
                 MaxGram = 16,
                 Side = EdgeNGramTokenFilterSide.Front,
             },
         },
         Analyzers = new Analyzer[]
         {
             new CustomAnalyzer
             {
                 Name = "name_search",
                 CharFilters = new []
                 {
                     CharFilterName.Create("remove_generics"),
                     CharFilterName.Create("period_to_space"),
                 },
                 Tokenizer = TokenizerName.Whitespace,
                 TokenFilters = new []
                 {
                     TokenFilterName.Lowercase,
                 },
             },
             new CustomAnalyzer
             {
                 Name = "name_index",
                 CharFilters = new []
                 {
                     CharFilterName.Create("remove_generics"),
                     CharFilterName.Create("period_to_space"),
                 },
                 Tokenizer = TokenizerName.Whitespace,
                 TokenFilters = new []
                 {
                     TokenFilterName.Lowercase,
                     TokenFilterName.Create("my_ngram"),
                 },
             },
             new CustomAnalyzer
             {
                 Name = "camel_hump_search",
                 Tokenizer = TokenizerName.Whitespace,
                 TokenFilters = new []
                 {
                     TokenFilterName.Lowercase,
                 },
             },
             new CustomAnalyzer
             {
                 Name = "camel_hump_index",
                 CharFilters = new []
                 {
                     CharFilterName.Create("remove_generics"),
                     CharFilterName.Create("remove_non_uppercase"),
                     CharFilterName.Create("period_to_empty_string"),
                 },
                 Tokenizer = TokenizerName.Keyword,
                 TokenFilters = new []
                 {
                     TokenFilterName.Lowercase,
                     TokenFilterName.Create("my_ngram"),
                 },
             },
         },
     });
 }
Пример #5
0
        private static AnalyzerName FromLuceneAnalyzer(string analyzer)
        {
            //not fully qualified, just return the type
            if (!analyzer.Contains(","))
            {
                return(AnalyzerName.Create(analyzer));
            }

            //if it contains a comma, we'll assume it's an assembly typed name

            if (analyzer.Contains("StandardAnalyzer"))
            {
                return(AnalyzerName.StandardLucene);
            }
            if (analyzer.Contains("WhitespaceAnalyzer"))
            {
                return(AnalyzerName.Whitespace);
            }
            if (analyzer.Contains("SimpleAnalyzer"))
            {
                return(AnalyzerName.Simple);
            }
            if (analyzer.Contains("KeywordAnalyzer"))
            {
                return(AnalyzerName.Keyword);
            }
            if (analyzer.Contains("StopAnalyzer"))
            {
                return(AnalyzerName.Stop);
            }

            if (analyzer.Contains("ArabicAnalyzer"))
            {
                return(AnalyzerName.ArLucene);
            }
            if (analyzer.Contains("BrazilianAnalyzer"))
            {
                return(AnalyzerName.PtBRLucene);
            }
            if (analyzer.Contains("ChineseAnalyzer"))
            {
                return(AnalyzerName.ZhHansLucene);
            }
            //if (analyzer.Contains("CJKAnalyzer")) //TODO: Not sure where this maps
            //    return AnalyzerName.ZhHansLucene;
            if (analyzer.Contains("CzechAnalyzer"))
            {
                return(AnalyzerName.CsLucene);
            }
            if (analyzer.Contains("DutchAnalyzer"))
            {
                return(AnalyzerName.NlLucene);
            }
            if (analyzer.Contains("FrenchAnalyzer"))
            {
                return(AnalyzerName.FrLucene);
            }
            if (analyzer.Contains("GermanAnalyzer"))
            {
                return(AnalyzerName.DeLucene);
            }
            if (analyzer.Contains("RussianAnalyzer"))
            {
                return(AnalyzerName.RuLucene);
            }

            //if the above fails, return standard
            return(AnalyzerName.StandardLucene);
        }
Пример #6
0
        public string GetSql()
        {
            StringBuilder sb = new StringBuilder();

            if (FieldName == "")
            {
                throw new Exception("FieldName can't be empty!");
            }

            if (checkBoxNull.Checked && DefaultValue == "" && IndexType != "None")
            {
                switch (comboBoxDataType.Text)
                {
                case "TinyInt":
                case "SmallInt":
                case "Int":
                case "BigInt":
                case "Float":
                case "DateTime":
                case "SmallDateTime":
                case "Date":
                    throw new Exception(string.Format("Nullabled Field:{0} must have a default value!", FieldName));
                }
            }

            sb.AppendFormat("{0} {1}", GetFieldName(FieldName), DataType);

            switch (DataType)
            {
            case "NVarchar":
            case "Varchar":
            case "NChar":
            case "Char":
                if (DataLength < 0)
                {
                    sb.Append("(max) ");
                }
                else
                {
                    sb.AppendFormat("({0}) ", DataLength);
                }
                break;

            default:
                sb.Append(" ");
                break;
            }

            switch (IndexType)
            {
            case "Tokenized":
                sb.AppendFormat("Tokenized Analyzer '{0}' ", AnalyzerName.Replace("'", "''"));
                break;

            case "Untokenized":
                sb.Append("Untokenized ");
                break;

            default:
                break;
            }

            if (IsNull)
            {
                sb.Append("NULL ");
            }
            else
            {
                sb.Append("NOT NULL ");
            }

            if (DefaultValue != "" || (IsNull && IndexType != "None"))
            {
                switch (DataType)
                {
                case "TinyInt":
                    byte.Parse(DefaultValue);
                    sb.AppendFormat("default {0} ", DefaultValue);
                    break;

                case "SmallInt":
                    Int16.Parse(DefaultValue);
                    sb.AppendFormat("default {0} ", DefaultValue);
                    break;

                case "Int":
                    int.Parse(DefaultValue);
                    sb.AppendFormat("default {0} ", DefaultValue);
                    break;

                case "BigInt":
                    long.Parse(DefaultValue);
                    sb.AppendFormat("default {0} ", DefaultValue);
                    break;

                case "DateTime":
                case "SmallDateTime":
                case "Date":
                    DateTime.Parse(DefaultValue);
                    sb.AppendFormat("default '{0}' ", DefaultValue.Replace("'", "''"));
                    break;

                case "Float":
                    float.Parse(DefaultValue);
                    sb.AppendFormat("default {0} ", DefaultValue);
                    break;

                case "NVarchar":
                case "Varchar":
                case "NChar":
                case "Char":
                    sb.AppendFormat("default '{0}' ", DefaultValue.Replace("'", "''"));
                    break;

                default:
                    break;
                }

                if (IsPK)
                {
                    sb.Append("Primary Key");
                }
            }

            return(sb.ToString());
        }