Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get this error in a RavenDB index query: The field 'CustomerId' is not indexed

This is my index code:

public class InvoiceSummaryView
{
    public DateTime DueDate { get; set; }
    public string CompanyAddress { get; set; }
    public string DebtorName { get; set; }
    public float Amount { get; set; }
    public bool IsPaid { get; set; }
    public string CustomerId { get; set; }
}

public class InvoiceSummaryIndex : AbstractIndexCreationTask<CustomerInvoice>
{
    public InvoiceSummaryIndex()
    {

        Map = invoices => from invoice in invoices
                          select new { DueDate = invoice.DueDate, DebtorId = invoice.DebtorId, Amount = invoice.Amount };

        TransformResults = (database, results) =>
            from invoice in results
            let debtor = database.Load<Debtor>(invoice.DebtorId)
            let company = database.Load<Company>(debtor.CompanyId)              
            select new {
                DueDate = invoice.DueDate,
                CompanyAddress = Company.Address.ToString(),
                DebtorName = debtor.Contact.First + " " + debtor.Contact.Last,
                Amount = invoice.Amount,
                IsPaid = invoice.IsPaid,
                CustomerId = Company.CustomerId
            };

    }
}

And this is my query:

var query = from viewItem in session.Query<InvoiceSummaryView>("InvoiceSummaryIndex")
                        where viewItem.CustomerId == id
                        orderby viewItem.DueDate
                        select viewItem;

the error is:

"Error": "System.ArgumentException: The field 'CustomerId' is not indexed, cannot query on fields that are not indexed at ...

like image 226
Idle Mach Avatar asked Jan 18 '23 13:01

Idle Mach


1 Answers

Look at your index:

   select new { DueDate = invoice.DueDate, DebtorId = invoice.DebtorId, Amount = invoice.Amount };

The fields that you have indexed are DueDate, DebtorId and Amount, that is it.

If you'll it it like this:

   select new { DueDate = invoice.DueDate, DebtorId = invoice.DebtorId, Amount = invoice.Amount, invoice.CustomerId };

It will work

like image 99
Ayende Rahien Avatar answered Jan 20 '23 02:01

Ayende Rahien