Escaping special characters in elasticsearch

2024/9/16 23:17:07

I am using the elasticsearch python client to make some queries to the elasticsearch instance that we are hosting.

I noticed that some characters need to be escaped. Specifically, these...

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \

Is there a clean way to do this beyond what I've already got in mind? Surely there is a cleaner way than doing

term.replace("+", "\+").replace("-", "\-")# ....etc

I was hoping there was an API call that I could use, but I can't find one in the docs. This seems like a common enough issue that it should have been solved by someone.

Does anyone know the "correct" way of doing this?

EDIT : I am still not sure if there is an API call, but I got things concise enough to where I am happy.

def needs_escaping(character):                                                                                                                                                                                        escape_chars = {                                                                                                                                                                                               '\\' : True, '+' : True, '-' : True, '!' : True,                                                                                                                                                           '(' : True, ')' : True, ':' : True, '^' : True,                                                                                                                                                            '[' : True, ']': True, '\"' : True, '{' : True,                                                                                                                                                            '}' : True, '~' : True, '*' : True, '?' : True,                                                                                                                                                            '|' : True, '&' : True, '/' : True                                                                                                                                                                         }                                                                                                                                                                                                              return escape_chars.get(character, False)   sanitized = ''
for character in query:                                                                                                                                                                                            if needs_escaping(character):                                                                                                                                                                                 sanitized += '\\%s' % character                                                                                                                                                                           else:                                                                                                                                                                                                      sanitized += character 
Answer

Yes, those characters will need to be replaced within content you want to search in a query_string query. To do that (assuming you are using PyLucene), you should be able to use QueryParserBase.escape(String).

Barring that, you could always adapt the QueryParserBase.escape source code to your needs:

public static String escape(String s) {StringBuilder sb = new StringBuilder();for (int i = 0; i < s.length(); i++) {char c = s.charAt(i);// These characters are part of the query syntax and must be escapedif (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'|| c == '*' || c == '?' || c == '|' || c == '&' || c == '/') {sb.append('\\');}sb.append(c);}return sb.toString();
}
https://en.xdnf.cn/q/72725.html

Related Q&A

Interacting with live matplotlib plot

Im trying to create a live plot which updates as more data is available.import os,sys import matplotlib.pyplot as pltimport time import randomdef live_plot():fig = plt.figure()ax = fig.add_subplot(111)…

pandas groupby: can I select an agg function by one level of a column MultiIndex?

I have a pandas DataFrame with a MultiIndex of columns:columns=pd.MultiIndex.from_tuples([(c, i) for c in [a, b] for i in range(3)]) df = pd.DataFrame(np.random.randn(4, 6),index=[0, 0, 1, 1],columns=c…

Bottle web app not serving static css files

My bottle web application is not serving my main.css file despite the fact I am using the static_file method.app.pyfrom bottle import * from xml.dom import minidom @route(/) def index():return template…

How to wrap text in OpenCV when I print it on an image and it exceeds the frame of the image?

I have a 1:1 ratio image and I want to make sure that if the text exceeds the frame of the image, it gets wrapped to the next line. How would I do it?I am thinking of doing an if-else block, where &qu…

pandas series filtering between values

If s is a pandas.Series, I know I can do this:b = s < 4or b = s > 0but I cant dob = 0 < s < 4orb = (0 < s) and (s < 4)What is the idiomatic pandas method for creating a boolean series…

python os.path.exists reports False when files is there

Hi have an application which is sometimes reporting that a file does not exist even when it does, I am using os.path.exists and the file is on a mounted network share. I am on OSX Yosemite, python 2.7.…

Python unhashable type: numpy.ndarray

I worked on making functions for K Nearest Neighbors. I have tested each function separately and they all work well. However whenever I put them together and run KNN_method, it shows unhashable type: n…

Efficient way to generate Lime explanations for full dataset

Am working on a binary classification problem with 1000 rows and 15 features. Currently am using Lime to explain the predictions of each instance. I use the below code to generate explanations for full…

how to handle javascript alerts in selenium using python

So I there is this button I want to click and if its the first time youve clicked it. A javascript alert popup will appear. Ive been using firebug and just cant find where that javascript is located an…

testing.postgresql command not found: initdb inside docker

Hi im trying to make a unittest with postgresql database that use sqlalchemy and alembicAlso im running it on docker postgresqlIm following the docs of testing.postgresql(docs) to set up a temporary po…