question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Util Method For Lucene Escape

See original GitHub issue

Often I find myself escaping the queries prior to executing them. I was using lucene-escape-query, but realized it isn’t quite right. I was going to create a pull request for that repo, but it seems like such a common thing that it would be beneficial to be included in this client library.

How are others handling this right now? Is there existing way or another package util people are using to do this?

What I was thinking is a simple util method as follows could be added to this client library…

// Based on https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-query-string-query.html#_reserved_characters
// TODO: This might not be exactly right
function escape (query) {
  return query.replace(/([\!\*\+\&\|\(\)\[\]\{\}\^\~\?\:\"\/])/g, "\\$1");
}

Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
vitorbaptistacommented, May 25, 2016

Thanks a lot, @spalger. You’ve given me lots of food for thought 😄

I still need to use the query string for now, because the API doesn’t receive the filters separated like that, but inside a q parameter. However, I’ll probably start adding those filters explictly as API parameters, then your suggestion will come in handy 👍

0reactions
GuillaumeCiscocommented, Aug 9, 2022

Done simply with lucene by using :

// https://lucene.apache.org/core/6_0_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Escaping_Special_Characters
const toEscape = [
  '+',
  '-',
  '&',
  '|',
  '!',
  '(',
  ')',
  '{',
  '}',
  '[',
  ']',
  '^',
  '"',
  '~',
  '*',
  '?',
  ':',
  '\\',
  '/',
]

const luceneEscape = (str) => {
  let res = ''
  for (let i = 0; i < str.length; i += 1) {
    const char = str.charAt(i)
    if (toEscape.includes(char)) {
      // handle special case && and ||
      if (['&', '|'].includes(char)) {
        if (char === str.charAt(i + 1)) {
          res = `${res}\\${char}\\${str.charAt(i + 1)}`
          i += 1
        } else res = `${res}${char}`
      } else res = `${res}\\${char}`
    } else res = `${res}${char}`
  }

  return res
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

QueryParserUtil (Lucene 3.0.3 API)
This class defines utility methods to (help) parse query strings into Query objects ... that TextParser expects to be escaped are escaped by...
Read more >
how can i escape a group of special characters in java in one ...
i am having problem escaping these characters because they are too many and if i use the String.replaceAll() method, i'll just end up...
Read more >
org.apache.lucene.queryparser.flexible.standard ... - Tabnine
Returns a String where those characters that TextParser expects to be escaped are escaped by a preceding \ . Popular methods of QueryParserUtil....
Read more >
KeywordsUtil (Liferay 6.1.2-ce-ga3 API)
com.liferay.util.lucene. Class KeywordsUtil ; escape · text) ; toFuzzy · keywords) ; toWildcard · keywords) ...
Read more >
org.apache.lucene.queryparser.classic.QueryParserBase ...
This page shows Java code examples of org.apache.lucene.queryparser.classic.QueryParserBase#escape.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found