Richard
Richard

Reputation: 69

RavenDB InvalidOperationException "The query results type is '<Document>' but you expected to get results of type '<IndexResult>' "

I am trying to follow the Indexing Tutorial "Example II" for a DocumenQuery from https://ravendb.net/docs/article-page/3.5/csharp/indexes/map-indexes

And get the following InvalidOperationException:

"The query results type is 'Resource' but you expected to get results of type 'Result'. If you want to return a projection, you should use .ProjectFromIndexFieldsInto() (for Query) or .SelectFields() (for DocumentQuery) before calling to .ToList()."

I fail how my setup is different from the documentation.

I have an index setup like this:

    public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>
    {
        public class Result
        {
            public string[] Query { get; set; }
        }


        public ResourceIndex()
        {
            Map = resources => from resource in resources
                               select new
                               {
                                   Query = new[]
                                    {
                                        resource.ID.ToString(),
                                    }
                               };

            Index("Query", FieldIndexing.Analyzed);
        }
    }

...

And query it like this:

    public IEnumerable<Raven.Resource> QueryAssets(string searchTerm)
    {
        using (IDocumentSession session = dataStore.OpenSession())
        {
            var resources = session
                .Advanced
                .DocumentQuery<ResourceIndex.Result, ResourceIndex>()
                .Search(x => x.Query, searchTerm).OfType<Raven.Resource>().ToList();

            return resources;
        }
    }

What am i missing in my setup vs the documentation about how to use an index in combination whith DocumentQuery and .Search?

Upvotes: 1

Views: 288

Answers (2)

Pure.Krome
Pure.Krome

Reputation: 86957

This might be a bit late to the party, but I thought the reason for the error is this:

You've declared:

public class ResourceIndex : AbstractIndexCreationTask<Raven.Resource>

this says: Create an Index, based off the document Raven.Resource and it will end up returning results that are of type Raven.Resource.

and then in your code, to use this index, you've said:

.DocumentQuery<ResourceIndex.Result, ResourceIndex>()

this says: Query the Index ResourceIndex and I expect ResourceIndex.Result documents to be returned.

But ... this is wrong. Then index contains Raven.Resource documents, not ResourceIndex.Result documents.

Upvotes: 0

Idan Haim Shalom
Idan Haim Shalom

Reputation: 1244

You just need to add after Search .SelectField<Raven.Resource>() and it will work like the exception suggested. Your query should look something like this

var resources = session
                        .Advanced
                        .DocumentQuery<ResourceIndex.Result, ResourceIndex>()
                        .Search(x => x.Query, searchTerm).SelectFields<Raven.Resource>().ToList();

You didn't miss anything in the documentation for now just remove the OfType() and use only the SelectFields
For the Query OfType works the same as SelectFields so if you use this your Query suppose to look like this:

session.Query<ResourceIndex.Result, ResourceIndex>()
.Search(x => x.Query, "2").OfType<Resource>().ToList();

Upvotes: 1

Related Questions