Esempio n. 1
0
        public void Indexing_func_must_return_anonymous_object()
        {
            var ex = Assert.Throws <IndexCompilationException>(() => IndexCompiler.Compile(new Bad_index_1().CreateIndexDefinition()));

            Assert.Contains("Indexing function must return an anonymous object", ex.Message);

            ex = Assert.Throws <IndexCompilationException>(() => IndexCompiler.Compile(new Bad_index_2().CreateIndexDefinition()));
            Assert.Contains("Indexing function must return an anonymous object", ex.Message);
        }
Esempio n. 2
0
        public void Convert_select_many_will_keep_doc_id()
        {
            var indexDefinition = new IndexDefinitionBuilder <Order>
            {
                Map = orders => from order in orders
                      from line in order.OrderLines
                      select new { line.ProductId }
#pragma warning disable CS0618 // Type or member is obsolete
            }.ToIndexDefinition(new DocumentConventions {
                PrettifyGeneratedLinqExpressions = false
            });

#pragma warning restore CS0618 // Type or member is obsolete

            indexDefinition.Name = "Index1";
            var index = IndexCompiler.Compile(indexDefinition);

            var map = index.Maps.Values.First().First();

            using (var context = JsonOperationContext.ShortTermSingleUse())
            {
                var results = map(new[]
                {
                    GetDocumentFromString(
                        @"
                {
                    '@metadata': {'@collection': 'Orders', '@id': 1},
                    'OrderLines': [{'ProductId': 2}, {'ProductId': 3}]
                }".Replace("\r\n", Environment.NewLine), context),
                    GetDocumentFromString(
                        @"
                {
                    '@metadata': {'@collection': 'Orders', '@id': 2},
                    'OrderLines': [{'ProductId': 5}, {'ProductId': 4}]
                }".Replace("\r\n", Environment.NewLine), context)
                }).Cast <object>().ToArray();

                var fields = index.OutputFields
                             .Select(x => IndexField.Create(x, new IndexFieldOptions(), null))
                             .ToList();

                var converter = new AnonymousLuceneDocumentConverter(fields, false);

                foreach (var result in results)
                {
                    using (var lazyStringValue = context.GetLazyString("docs/1"))
                    {
                        bool shouldSkip;
                        converter.SetDocument(lazyStringValue, result, context, out shouldSkip);
                        Assert.Equal("docs/1", converter.Document.Get(Constants.Documents.Indexing.Fields.DocumentIdFieldName, null));
                    }
                }
            }
        }
Esempio n. 3
0
        public void CanCompileComplexQuery()
        {
            var indexDefinition = new IndexDefinitionBuilder <Person>()
            {
                Map = people => from person in people
                      from role in person.Roles
                      where role == "Student"
                      select new { role }
            }.ToIndexDefinition(DocumentConventions.Default);

            indexDefinition.Name = "Index1";
            IndexCompiler.Compile(indexDefinition);
        }
Esempio n. 4
0
        public async Task PostIndexFields()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                using (var json = await context.ReadForMemoryAsync(RequestBodyStream(), "map"))
                {
                    if (json.TryGet("Map", out string map) == false)
                    {
                        throw new ArgumentException("'Map' field is mandatory, but wasn't specified");
                    }

                    json.TryGet(nameof(IndexDefinition.AdditionalSources), out BlittableJsonReaderObject additionalSourcesJson);

                    var indexDefinition = new IndexDefinition
                    {
                        Name = "index-fields",
                        Maps =
                        {
                            map
                        },
                        AdditionalSources = ConvertToAdditionalSources(additionalSourcesJson)
                    };

                    try
                    {
                        var compiledIndex = IndexCompiler.Compile(indexDefinition);

                        var outputFields = compiledIndex.OutputFields;

                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartObject();
                            writer.WriteArray(context, "Results", outputFields, (w, c, field) => { w.WriteString(field); });
                            writer.WriteEndObject();
                        }
                    }
                    catch (IndexCompilationException)
                    {
                        // swallow compilaton exception and return empty array as response

                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartArray();
                            writer.WriteEndArray();
                        }
                    }
                }
            }
        }
Esempio n. 5
0
 public void Sum_of_elements()
 {
     IndexCompiler.Compile(new IndexDefinition
     {
         Name = "test",
         Maps =
         {
             @"from order in docs.Orders select new { 
                     order.Company, 
                     Count = 1, 
                     Total = order.Lines.Sum(l => l.PricePerUnit)
             }"
         }
     });
 }
Esempio n. 6
0
        public void CanCompileComplexQuery()
        {
            var indexDefinition = new IndexDefinitionBuilder <Person>()
            {
                Map = people => from person in people
                      from role in person.Roles
                      where role == "Student"
                      select new { role }
            }.ToIndexDefinition(new DocumentConventions {
                PrettifyGeneratedLinqExpressions = false
            });

            indexDefinition.Name = "Index1";
            IndexCompiler.Compile(indexDefinition);
        }
Esempio n. 7
0
        public void CanCompileComplexQuery()
        {
            var indexDefinition = new IndexDefinitionBuilder <Person>()
            {
                Map = people => from person in people
                      from role in person.Roles
                      where role == "Student"
                      select new { role }
#pragma warning disable CS0618 // Type or member is obsolete
            }.ToIndexDefinition(new DocumentConventions {
                PrettifyGeneratedLinqExpressions = false
            });

#pragma warning restore CS0618 // Type or member is obsolete

            indexDefinition.Name = "Index1";
            IndexCompiler.Compile(indexDefinition);
        }