Exemplo n.º 1
0
        public void SelectFieldsFromIndex()
        {
            using (var store = GetDocumentStore())
            {
                var myObject = new
                {
                    name     = "ayende",
                    email    = "*****@*****.**",
                    projects = new  []
                    {
                        "rhino mocks",
                        "nhibernate",
                        "rhino service bus",
                        "rhino divan db",
                        "rhino persistent hash table",
                        "rhino distributed hash table",
                        "rhino etl",
                        "rhino security",
                        "rampaging rhinos"
                    }
                };

                store.Commands().Put("ayende", null, myObject);

                var fieldOptions = new IndexFieldOptions {
                    Storage = FieldStorage.Yes
                };

                store.Maintenance.Send(new PutIndexesOperation(new[] { new IndexDefinition
                                                                       {
                                                                           Maps   = { "from doc in docs from project in doc.projects select new {doc.email, doc.name, project };" },
                                                                           Name   = "EmailAndProject",
                                                                           Fields =
                                                                           {
                                                                               { "email",   fieldOptions },
                                                                               { "name",    fieldOptions },
                                                                               { "project", fieldOptions },
                                                                           }
                                                                       } }));


                while (store.Commands().Query(new IndexQuery {
                    Query = "FROM INDEX 'EmailAndProject'"
                }).IsStale)
                {
                    Thread.Sleep(100);
                }

                var queryResult = store.Commands().Query(new IndexQuery {
                    Query = "FROM INDEX 'EmailAndProject' as e SELECT e.email"
                });

                Assert.Equal(9, queryResult.Results.Length);

                foreach (BlittableJsonReaderObject result in queryResult.Results)
                {
                    Assert.Equal("*****@*****.**", result["email"].ToString());
                }
            }
        }
Exemplo n.º 2
0
        protected IEnumerable <AbstractField> CreateField(string name, object value, CreateFieldOptions options)
        {
            // IMPORTANT: Do not delete this method, it is used by the indexes code when using CreateField

            options = options ?? CreateFieldOptions.Default;

            IndexFieldOptions allFields = null;
            var scope = CurrentIndexingScope.Current;

            if (scope.IndexDefinition is MapIndexDefinition mapIndexDefinition)
            {
                mapIndexDefinition.IndexDefinition.Fields.TryGetValue(Constants.Documents.Indexing.Fields.AllFields, out allFields);
            }

            var field = IndexField.Create(name, new IndexFieldOptions
            {
                Storage    = options.Storage,
                TermVector = options.TermVector,
                Indexing   = options.Indexing
            }, allFields);

            if (scope.CreateFieldConverter == null)
            {
                scope.CreateFieldConverter = new LuceneDocumentConverter(new IndexField[] { });
            }

            var result = new List <AbstractField>();

            scope.CreateFieldConverter.GetRegularFields(new StaticIndexLuceneDocumentWrapper(result), field, value, CurrentIndexingScope.Current.IndexContext);
            return(result);
        }
Exemplo n.º 3
0
            public override IndexDefinition CreateIndexDefinition()
            {
                var fieldOptions = new IndexFieldOptions {
                    Storage = FieldStorage.Yes
                };

                return(new IndexDefinition
                {
                    Maps =
                    {
                        @"map(""Companies"", (company) => {
                            if (company.Fax < 10) {
                                var metadata = getMetadata(company)
                                return {
                                    Collection:     metadata[""@collection""],
                                    ChangeVector:   metadata[""@change-vector""],
                                    Id:             metadata[""@id""],
                                    LastModified:   metadata[""@last-modified""]
                                };
                            }
                        })"
                    },
                    Fields =
                    {
                        { nameof(Result.Collection),   fieldOptions },
                        { nameof(Result.ChangeVector), fieldOptions },
                        { nameof(Result.Id),           fieldOptions },
                        { nameof(Result.LastModified), fieldOptions },
                    }
                });
            }
Exemplo n.º 4
0
            public override IndexDefinition CreateIndexDefinition()
            {
                var fieldOptions = new IndexFieldOptions {
                    Storage = FieldStorage.Yes
                };

                return(new IndexDefinition
                {
                    Maps =
                    {
                        @"map(""Companies"", (company) => {
                            if (company.Fax < 10) {
                                return {
                                    Id: id(company),
                                    Name: company.Name,
                                    Fax: company.Fax
                                };
                            }
                        })"
                    },
                    Fields =
                    {
                        { nameof(Result.Id), fieldOptions },
                    }
                });
            }
Exemplo n.º 5
0
        public async Task Should_disable_index()
        {
            var fieldOptions = new IndexFieldOptions {
                Analyzer = typeof(ThrowingAnalyzerImpl).AssemblyQualifiedName
            };

            using (var store = GetDocumentStore())
            {
                store.Maintenance.Send(new PutIndexesOperation(new[] {
                    new IndexDefinition
                    {
                        Maps   = { "from doc in docs select new { doc.Name}" },
                        Fields = { { "Name", fieldOptions } },
                        Name   = "foo"
                    }
                }));

                for (var i = 0; i < 20; i++)
                {
                    using (var session = store.OpenSession())
                    {
                        session.Store(new User {
                            Name = "Ayende"
                        });
                        session.SaveChanges();
                    }

                    try
                    {
                        Indexes.WaitForIndexing(store);
                    }
                    catch
                    {
                    }
                }

                var fooIndex = store.Maintenance.Send(new GetStatisticsOperation()).Indexes.First(x => x.Name == "foo");

                Assert.True(fooIndex.State == IndexState.Error);

                var db = await Databases.GetDocumentDatabaseInstanceFor(store);

                for (int i = 0; i < 10; i++)
                {
                    if (db.IndexStore.GetIndexes().Sum(index => index.GetErrorCount()) > 0)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                var errorsCount = db.IndexStore.GetIndexes().Sum(index => index.GetErrorCount());

                Assert.NotEqual(errorsCount, 0);
            }
        }
Exemplo n.º 6
0
        public static IndexField Create(string name, IndexFieldOptions options, IndexFieldOptions allFields)
        {
            var field = new IndexField
            {
                Name     = name,
                Analyzer = options.Analyzer ?? allFields?.Analyzer
            };

            if (options.Indexing.HasValue)
            {
                field.Indexing = options.Indexing.Value;
            }
            else if (string.IsNullOrWhiteSpace(field.Analyzer) == false)
            {
                field.Indexing = FieldIndexing.Search;
            }
            else if (allFields?.Indexing != null)
            {
                field.Indexing = allFields.Indexing.Value;
            }

            if (options.Storage.HasValue)
            {
                field.Storage = options.Storage.Value;
            }
            else if (allFields?.Storage != null)
            {
                field.Storage = allFields.Storage.Value;
            }

            if (options.TermVector.HasValue)
            {
                field.TermVector = options.TermVector.Value;
            }
            else if (allFields?.TermVector != null)
            {
                field.TermVector = allFields.TermVector.Value;
            }

            if (options.Suggestions.HasValue)
            {
                field.HasSuggestions = options.Suggestions.Value;
            }
            else if (allFields?.Suggestions != null)
            {
                field.HasSuggestions = allFields.Suggestions.Value;
            }

            if (options.Spatial != null)
            {
                field.Spatial = new SpatialOptions(options.Spatial);
            }

            return(field);
        }
Exemplo n.º 7
0
            public override IndexDefinition CreateIndexDefinition()
            {
                var fieldOptions1 = new IndexFieldOptions {
                    Indexing = FieldIndexing.Exact, Storage = FieldStorage.Yes
                };
                var fieldOptions2 = new IndexFieldOptions {
                    Indexing = FieldIndexing.Search, Storage = FieldStorage.Yes
                };

                var index = new IndexDefinition
                {
                    Name = this.IndexName,

                    Maps =
                    {
                        @"
                        from doc in docs.WhereEntityIs(""Accounts"", ""Users"", ""Designs"")
                        let acc = doc[""@metadata""][""@collection""] == ""Accounts"" ? doc : null
                        let user = doc[""@metadata""][""@collection""] == ""Users"" ? doc : null
                        let design = doc[""@metadata""][""@collection""] == ""Designs"" ? doc : null
                        select new 
                        {
                            AccountId = acc != null ? acc.Id : (user != null ? user.AccountId : design.AccountId),
                            AccountName = acc != null ? acc.Name : null,
                            UserName = user != null ? user.Name : null,
                            DesignName = design != null ? design.Name : null
                        }"
                    },

                    Reduce =
                        @"
                        from result in results 
                        group result by result.AccountId into g
                        select new 
                        {
                            AccountId = g.Key,
                            AccountName = g.Where(x=>x.AccountName != null).Select(x=>x.AccountName).FirstOrDefault(),
                            UserName = g.Where(x=>x.UserName != null).Select(x=>x.UserName),
                            DesignName = g.Where(x=>x.DesignName != null).Select(x=>x.DesignName),
                        }",

                    Fields =
                    {
                        { "AccountId",   fieldOptions1 },
                        { "AccountName", fieldOptions2 },
                        { "DesignName",  fieldOptions2 },
                        { "UserName",    fieldOptions2 }
                    }
                };

                return(index);
            }
Exemplo n.º 8
0
        public void UsingKeywordAnalyzing()
        {
            using (var store = GetDocumentStore())
                using (var session = store.OpenSession())
                {
                    var fieldOptions1 = new IndexFieldOptions {
                        Indexing = FieldIndexing.Exact
                    };
                    var fieldOptions2 = new IndexFieldOptions {
                        Indexing = FieldIndexing.Exact,
                    };
                    var fieldOptions3 = new IndexFieldOptions {
                        Indexing = FieldIndexing.Search, Analyzer = typeof(KeywordAnalyzer).AssemblyQualifiedName
                    };

                    store.Admin.Send(new PutIndexesOperation(new[] { new IndexDefinition
                                                                     {
                                                                         Maps = { @"from s in docs.Softs select new { s.f_platform, s.f_name, s.f_alias,s.f_License,s.f_totaldownload}" },

                                                                         Fields =
                                                                         {
                                                                             { "f_platform",      fieldOptions1 },
                                                                             { "f_License",       fieldOptions2 },
                                                                             { "f_totaldownload", fieldOptions2 },
                                                                             { "f_name",          fieldOptions3 },
                                                                             { "f_alias",         fieldOptions3 }
                                                                         },
                                                                         Name = "test"
                                                                     } }));

                    Soft entity = new Soft
                    {
                        f_platform      = 1,
                        f_name          = "hello Shrek",
                        f_alias         = "world",
                        f_License       = "agpl",
                        f_totaldownload = -1
                    };
                    session.Store(entity);
                    session.Advanced.GetMetadataFor(entity)["@collection"] = "Softs";
                    session.SaveChanges();

                    List <Soft> tmps = session.Advanced.DocumentQuery <Soft>("test").
                                       WaitForNonStaleResults(TimeSpan.FromHours(1))
                                       .WhereStartsWith("f_name", "s")
                                       .OrderByDescending("f_License")
                                       .OrderBy("f_totaldownload")
                                       .ToList();

                    Assert.Empty(tmps);
                }
        }
Exemplo n.º 9
0
        private void ApplyValues <TValue>(IndexDefinition indexDefinition, IDictionary <string, TValue> values, Action <IndexFieldOptions, TValue> action)
        {
            foreach (var kvp in values)
            {
                IndexFieldOptions field;
                if (indexDefinition.Fields.TryGetValue(kvp.Key, out field) == false)
                {
                    indexDefinition.Fields[kvp.Key] = field = new IndexFieldOptions();
                }

                action(field, kvp.Value);
            }
        }
Exemplo n.º 10
0
        public async Task Should_give_clear_error()
        {
            var fieldOptions = new IndexFieldOptions {
                Analyzer = typeof(ThrowingAnalyzerImpl).AssemblyQualifiedName
            };

            using (var store = GetDocumentStore())
            {
                store.Maintenance.Send(new PutIndexesOperation(new[] {
                    new IndexDefinition
                    {
                        Maps   = { "from doc in docs select new { doc.Name}" },
                        Fields = { { "Name", fieldOptions } },
                        Name   = "foo"
                    }
                }));

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Ayende"
                    });
                    session.SaveChanges();
                }


                using (var session = store.OpenSession())
                {
                    Assert.Throws <RavenException>(() =>

                                                   session.Query <User>("foo")
                                                   .Customize(x => x.WaitForNonStaleResults())
                                                   .ToList()
                                                   );
                }

                var db = await Databases.GetDocumentDatabaseInstanceFor(store);

                for (int i = 0; i < 10; i++)
                {
                    if (db.IndexStore.GetIndexes().Sum(index => index.GetErrorCount()) > 0)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }
                var errorsCount = db.IndexStore.GetIndexes().Sum(index => index.GetErrorCount());

                Assert.NotEqual(errorsCount, 0);
            }
        }
Exemplo n.º 11
0
        public IndexFieldOptions ToIndexFieldOptions()
        {
            if (_indexFieldOptions != null)
            {
                return(_indexFieldOptions);
            }

            return(_indexFieldOptions = new IndexFieldOptions
            {
                Analyzer = Analyzer,
                Indexing = Indexing,
                Storage = Storage,
                TermVector = TermVector
            });
        }
Exemplo n.º 12
0
        public static IndexField Create(string name, IndexFieldOptions options, IndexFieldOptions allFields)
        {
            var field = new IndexField();

            field.Name     = name;
            field.Analyzer = options.Analyzer ?? allFields?.Analyzer;

            if (options.Indexing.HasValue)
            {
                field.Indexing = options.Indexing.Value;
            }
            else if (string.IsNullOrWhiteSpace(field.Analyzer) == false)
            {
                field.Indexing = FieldIndexing.Analyzed;
            }

            if (options.Sort.HasValue)
            {
                field.SortOption = options.Sort.Value;
            }
            else if (allFields?.Sort != null)
            {
                field.SortOption = allFields.Sort.Value;
            }

            if (options.Storage.HasValue)
            {
                field.Storage = options.Storage.Value;
            }
            else if (allFields?.Storage != null)
            {
                field.Storage = allFields.Storage.Value;
            }

            if (options.TermVector.HasValue)
            {
                field.TermVector = options.TermVector.Value;
            }
            else if (allFields?.TermVector != null)
            {
                field.TermVector = allFields.TermVector.Value;
            }

            // options.Suggestions // TODO [ppekrol]
            // options.Spatial // TODO [ppekrol]

            return(field);
        }
Exemplo n.º 13
0
        public IndexFieldOptions ToIndexFieldOptions()
        {
            if (_indexFieldOptions != null)
            {
                return(_indexFieldOptions);
            }

            return(_indexFieldOptions = new IndexFieldOptions
            {
                Analyzer = Analyzer,
                Indexing = Indexing,
                Storage = Storage,
                TermVector = TermVector,
                Suggestions = HasSuggestions,
                Spatial = Spatial
            });
        }
Exemplo n.º 14
0
        public void ShouldSearchCorrectly()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new Item {
                        Text = "Seek's"
                    });
                    session.Store(new Item {
                        Text = "Sit"
                    });

                    session.SaveChanges();
                }
                var opt = new IndexFieldOptions {
                    Analyzer = typeof(Lucene.Net.Analysis.Standard.StandardAnalyzer).AssemblyQualifiedName, Indexing = FieldIndexing.Search
                };

                store.Maintenance.Send(new PutIndexesOperation(new[] { new IndexDefinition
                                                                       {
                                                                           Name = "test",
                                                                           Maps = new HashSet <string> {
                                                                               "from doc in docs select new { doc.Text }"
                                                                           },
                                                                           Fields = { { "Text", opt } },
                                                                       } }));

                using (var session = store.OpenSession())
                {
                    Assert.NotEmpty(session.Query <Item>("test")
                                    .Customize(x => x.WaitForNonStaleResults())
                                    .Search(x => x.Text, "Seek")
                                    .ToList());

                    Assert.NotEmpty(session.Query <Item>("test")
                                    .Customize(x => x.WaitForNonStaleResults())
                                    .Search(x => x.Text, "Sit's")
                                    .ToList());
                }
            }
        }
Exemplo n.º 15
0
        public void ShouldSearchCorrectly()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new Item {
                        Text = "Seek's"
                    });
                    session.Store(new Item {
                        Text = "Sit"
                    });

                    session.SaveChanges();
                }
                var opt = new IndexFieldOptions {
                    Analyzer = typeof(StandardAnalyzer).AssemblyQualifiedName, Indexing = FieldIndexing.Analyzed
                };

                store.DatabaseCommands.PutIndex("test", new IndexDefinition
                {
                    Maps = new HashSet <string> {
                        "from doc in docs select new { doc.Text }"
                    },
                    Fields = { { "Text", opt } },
                });

                using (var session = store.OpenSession())
                {
                    Assert.NotEmpty(session.Query <Item>("test")
                                    .Customize(x => x.WaitForNonStaleResults())
                                    .Where(x => x.Text == "Seek")
                                    .ToList());

                    Assert.NotEmpty(session.Query <Item>("test")
                                    .Customize(x => x.WaitForNonStaleResults())
                                    .Where(x => x.Text == "Sit's")
                                    .ToList());
                }
            }
        }
Exemplo n.º 16
0
        public void Should_disable_index()
        {
            var fieldOptions = new IndexFieldOptions {
                Analyzer = typeof(ThrowingAnalyzerImpl).AssemblyQualifiedName
            };

            using (var store = GetDocumentStore())
            {
                store.Admin.Send(new PutIndexesOperation(new[] {
                    new IndexDefinition
                    {
                        Maps   = { "from doc in docs select new { doc.Name}" },
                        Fields = { { "Name", fieldOptions } },
                        Name   = "foo"
                    }
                }));

                for (var i = 0; i < 20; i++)
                {
                    using (var session = store.OpenSession())
                    {
                        session.Store(new User {
                            Name = "Ayende"
                        });
                        session.SaveChanges();
                    }

                    Assert.Throws <RavenException>(() => WaitForIndexing(store));
                }

                var fooIndex = store.Admin.Send(new GetStatisticsOperation()).Indexes.First(x => x.Name == "foo");

                Assert.True(fooIndex.State == IndexState.Error);

                var db          = GetDocumentDatabaseInstanceFor(store).Result;
                var errorsCount = db.IndexStore.GetIndexes().Sum(index => index.GetErrorCount());

                Assert.NotEqual(errorsCount, 0);
            }
        }
        private static void WriteIndexFieldOptions(this BlittableJsonTextWriter writer, JsonOperationContext context, IndexFieldOptions options, bool removeAnalyzers)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(options.Analyzer));
            if (string.IsNullOrWhiteSpace(options.Analyzer) == false && !removeAnalyzers)
            {
                writer.WriteString(options.Analyzer);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Indexing));
            if (options.Indexing.HasValue)
            {
                writer.WriteString(options.Indexing.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Storage));
            if (options.Storage.HasValue)
            {
                writer.WriteString(options.Storage.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Suggestions));
            if (options.Suggestions.HasValue)
            {
                writer.WriteBool(options.Suggestions.Value);
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.TermVector));
            if (options.TermVector.HasValue)
            {
                writer.WriteString(options.TermVector.ToString());
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteComma();

            writer.WritePropertyName(nameof(options.Spatial));
            if (options.Spatial != null)
            {
                writer.WriteStartObject();

                writer.WritePropertyName(nameof(options.Spatial.Type));
                writer.WriteString(options.Spatial.Type.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxTreeLevel));
                writer.WriteInteger(options.Spatial.MaxTreeLevel);
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxX));
                LazyStringValue lazyStringValue;
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MaxY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MaxY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinX));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinX)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.MinY));
                using (lazyStringValue = context.GetLazyString(CharExtensions.ToInvariantString(options.Spatial.MinY)))
                    writer.WriteDouble(new LazyNumberValue(lazyStringValue));
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Strategy));
                writer.WriteString(options.Spatial.Strategy.ToString());
                writer.WriteComma();

                writer.WritePropertyName(nameof(options.Spatial.Units));
                writer.WriteString(options.Spatial.Units.ToString());

                writer.WriteEndObject();
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
        private static IEnumerable <SyntaxNodeOrToken> InnerParsingIndexDefinitionFieldsToRoslyn(IndexFieldOptions options)
        {
            var syntaxNodeOrToken = new List <SyntaxNodeOrToken>();

            if (options.Indexing != null)
            {
                AddCommaTokenIfNecessary(syntaxNodeOrToken);
                syntaxNodeOrToken.Add(ParseEnum(options.Indexing, nameof(options.Indexing)));
            }
            if (options.Storage != null)
            {
                AddCommaTokenIfNecessary(syntaxNodeOrToken);
                syntaxNodeOrToken.Add(ParseEnum(options.Storage, nameof(options.Storage)));
            }
            if (options.TermVector != null)
            {
                AddCommaTokenIfNecessary(syntaxNodeOrToken);
                syntaxNodeOrToken.Add(ParseEnum(options.TermVector, nameof(options.TermVector)));
            }
            if (!string.IsNullOrEmpty(options.Analyzer))
            {
                AddCommaTokenIfNecessary(syntaxNodeOrToken);
                syntaxNodeOrToken.Add(GetLiteral(nameof(options.Analyzer), options.Analyzer));
            }
            if (options.Suggestions != null)
            {
                AddCommaTokenIfNecessary(syntaxNodeOrToken);
                syntaxNodeOrToken.Add(ParseBool((bool)options.Suggestions, nameof(options.Suggestions)));
            }

            return(syntaxNodeOrToken);
        }