Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid query translation (where/search combination) #18446

Closed
elitastic opened this issue Apr 26, 2024 · 4 comments
Closed

Invalid query translation (where/search combination) #18446

elitastic opened this issue Apr 26, 2024 · 4 comments
Assignees

Comments

@elitastic
Copy link

With a certain combination of a where/search query, the query does not seem to be translated correctly:

The following query:

session
    .Query<TestDocumentIndex.Result, TestDocumentIndex>()
    .Where(d => d.Category == "A" || d.Category == "B")
    .Search(d => d.Name, "Peter*")

is translated into

from index 'TestDocumentIndex' where Category = $p0 or Category = $p1 and search(Name, $p2)

It looks like the braces are set incorrectly? Below is a failing test that demonstrates the problem:

Tested versions: 5.4.117 and 6.0.101

using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Linq;
using Raven.TestDriver;
using Xunit;

namespace RavenUnitTests;

public class RavenBug : RavenTestDriver
{
    [Fact]
    public void WhereSearchCombination()
    {
        using var store = GetDocumentStore();

        store.ExecuteIndex(new TestDocumentIndex());

        using (var session = store.OpenSession())
        {
            session.Store(new TestDocument { Category = "A", Name = "Peter Fox" });
            session.Store(new TestDocument { Category = "B", Name = "Peter Balzli" });
            session.Store(new TestDocument { Category = "A", Name = "John Smith" });
            session.SaveChanges();
        }

        WaitForIndexing(store);

        using (var session = store.OpenSession())
        {
            var documents = session
                .Query<TestDocumentIndex.Result, TestDocumentIndex>()
                .Where(d => d.Category == "A" || d.Category == "B")
                .Search(d => d.Name, "Peter*")
                .As<TestDocument>()
                .ToList();

            Assert.Equal(2, documents.Count);
            Assert.Single(documents, d => d.Name == "Peter Fox");
            Assert.Single(documents, d => d.Name == "Peter Balzli");
        }
    }

    private class TestDocumentIndex : AbstractIndexCreationTask<TestDocument, TestDocumentIndex.Result>
    {
        public class Result
        {
            public string Category { get; init; }
            public string Name { get; init; }
        }

        public TestDocumentIndex()
        {
            Map = documents => from document in documents
                               select new
                               {
                                   document.Category,
                                   document.Name
                               };

            Index(x => x.Name, FieldIndexing.Search);
        }
    }

    private class TestDocument
    {
        public string Category { get; init; }
        public string Name { get; init; }
    }
}
@ayende
Copy link
Member

ayende commented Apr 26, 2024

That certainly looks like a bug, yes.

@Lwiel
Copy link
Contributor

Lwiel commented Apr 29, 2024

I've created an issue for this, we'll fix the translation.
https://issues.hibernatingrhinos.com/issue/RavenDB-22323/Parentheses-missing-when-combining-Where-and-Search-clause

Thanks for noticing this bug!

@elitastic
Copy link
Author

Thank you @Lwiel!

@Lwiel
Copy link
Contributor

Lwiel commented May 15, 2024

Fixed in #18516

@Lwiel Lwiel closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants