This is an automated email from the ASF dual-hosted git repository.
humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git
The following commit(s) were added to refs/heads/master by this push:
new 827c283 Enhance search defuzzing
827c283 is described below
commit 827c2833d5c7a22861ef9089a8fdf531663fd3aa
Author: Daniel Gruno <[email protected]>
AuthorDate: Sat Oct 16 21:40:30 2021 +0200
Enhance search defuzzing
switch to multi_match within a should bool array, with a
minimum_should_match matching the number of terms.
This gets rid of escaping phrases and special characters.
---
server/plugins/defuzzer.py | 68 ++++++++++++++++++++++++++++++----------------
1 file changed, 45 insertions(+), 23 deletions(-)
diff --git a/server/plugins/defuzzer.py b/server/plugins/defuzzer.py
index 8fd1b34..0689b73 100644
--- a/server/plugins/defuzzer.py
+++ b/server/plugins/defuzzer.py
@@ -121,7 +121,6 @@ def defuzz(formdata: dict, nodate: bool = False,
list_override: typing.Optional[
# - "this sentence": find emails with this exact string
if "q" in formdata:
qs = formdata["q"].replace(":", "")
- qs = qs.replace("/", " ") # Forward slashes are reserved characters
bits = shlex.split(qs)
should = []
@@ -135,35 +134,57 @@ def defuzz(formdata: dict, nodate: bool = False,
list_override: typing.Optional[
bit = bit[1:]
# Negatives
if bit[0] == "-" and not force_positive:
- # Quoted?
- if bit.find(" ") != -1:
- bit = '"' + bit + '"'
shouldnot.append(bit[1:])
# Positives
else:
- # Quoted?
- if bit.find(" ") != -1:
- bit = '"' + bit + '"'
should.append(bit)
- if len(should) > 0:
- must.append(
- {
- "query_string": {
- "fields": ["subject", "from", "body"],
- "query": " AND ".join(should),
+ if should:
+ xshould = []
+ for x in should:
+ xshould.append(
+ {
+ "bool": {
+ "should":
+ [
+ {
+ "multi_match": {
+ "fields": ["from", "body",
"subject"],
+ "query": x,
+ "type": "phrase"
+ },
+ },
+
+ ]
+
+ }
}
- }
- )
- if len(shouldnot) > 0:
- must_not.append(
- {
- "query_string": {
- "fields": ["subject", "from", "body"],
- "query": " AND ".join(shouldnot),
+ )
+ xmust = {"bool": {"minimum_should_match": len(should), "should":
xshould}}
+ must.append(xmust)
+ if shouldnot:
+ for x in shouldnot:
+ must_not.append(
+ {
+ "match": {
+ "subject": x,
+ }
}
- }
- )
+ )
+ must_not.append(
+ {
+ "match": {
+ "from": x,
+ }
+ }
+ )
+ must_not.append(
+ {
+ "match": {
+ "body": x,
+ }
+ }
+ )
# Header parameters
for header in ["from", "subject", "body", "to"]:
@@ -174,6 +195,7 @@ def defuzz(formdata: dict, nodate: bool = False,
list_override: typing.Optional[
must.append({"match": {header: {"query": hvalue}}})
thebool = {"must": must}
+ print(thebool)
if len(must_not) > 0:
thebool["must_not"] = must_not