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

Unable to parse query with escaped characters #49

Closed
jonnermut opened this issue Jun 12, 2020 · 5 comments
Closed

Unable to parse query with escaped characters #49

jonnermut opened this issue Jun 12, 2020 · 5 comments
Assignees
Labels

Comments

@jonnermut
Copy link

jonnermut commented Jun 12, 2020

ElasticSearch / Lucene allows escaping characters with a backslash:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

Feeding queries with backslashed characters in into a ElasticQueryParser them fails to parse with an exception. The same query string works when used in ElasticSearch directly. Test case below:

using System.Diagnostics;
using System.Threading.Tasks;
using Foundatio.Parsers.ElasticQueries;
using Foundatio.Parsers.ElasticQueries.Visitors;
using Foundatio.Parsers.LuceneQueries.Visitors;

namespace QueryEscapingTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var simple = ParseAndRewrite("normal").Result;
            Debug.Assert(simple == "normal");

            var escaped = ParseAndRewrite("\\\"escaped").Result;
            Debug.Assert(escaped == "\\\"escaped");
        }

        static async Task<string> ParseAndRewrite(string query)
        {
            QueryFieldResolver resolver = (field) =>
            {
                return field;
            };

            ElasticQueryVisitorContext context = new ElasticQueryVisitorContext { QueryType = QueryType.Query };

            var parser = new ElasticQueryParser(conf => 
                conf.UseFieldResolver(resolver)
                    .UseValidation(async info => true)
            );

            var queryNode = await parser.ParseAsync(query, context);
            return queryNode.ToString();
        }

    }
}

Project file:
QueryEscapingTest.zip

@ejsmith
Copy link
Contributor

ejsmith commented Jun 20, 2020

@jonnermut this should be fixed now. Thanks for reporting!

@jonnermut
Copy link
Author

Awesome, thanks!

@InsomniumBR
Copy link

@ejsmith could you let me know if I am doing something wrong according to the Lucene syntax here?
I grabbed the 4th line from the original bug and just moved the backslash to the end of the string.
The first 3 lines relate to what I was doing before seeing this old bug.

            var parser = new LuceneQueryParser();
            _ = parser.Parse("a:123"); // OK
            _ = parser.Parse("a:\"123\""); // OK
            _ = parser.Parse("a:\"123\\\""); // Exception: Unterminated quoted string

            _ = parser.Parse("\\\"escaped");  // OK
            _ = parser.Parse("\"escaped\\"); // Exception: Unterminated quoted string

Thank you

@niemyjski
Copy link
Member

niemyjski commented Oct 9, 2024

Would you mind creating a new issue for this @InsomniumBR. The first one looks like it could be a bug, the last one I'm wondering how does that even compile. If you could add a pr or updates to the existing test in a snippet that would be a huge help.

@avillarrealRW
Copy link

Hello @niemyjski, I'm a coworker of @InsomniumBR and was helping out with adding some tests based on the issues we were running into. Here is the PR that I created for that, thank you!
#92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants