Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
instification committed Jan 25, 2023
1 parent c91fb6e commit b05900d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/collective/elasticsearch/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def _make_simple_query_string(self, query):
query = query.replace(" AND ", " + ")
return query


def get_query(self, name, value):
value = self._normalize_query(value)
# ES doesn't care about * like zope catalog does
Expand All @@ -224,7 +223,11 @@ def get_query(self, name, value):
if name in ("Title", "SearchableText"):
# titles have most importance... we override here...
queries.append(
{"match_phrase_prefix": {"Title": {"query": clean_value, "boost": 2}}}
{
"match_phrase_prefix": {
"Title": {"query": clean_value, "boost": 2}
}
}
)
if name != "Title":
queries.append({"match": {name: {"query": clean_value}}})
Expand All @@ -239,16 +242,10 @@ def get_query(self, name, value):
fields.extend(["Title^2", "SearchableText"])
else:
fields.append(name)
query = {
"simple_query_string": {
"query": qs_value,
"fields": fields
}
}
query = {"simple_query_string": {"query": qs_value, "fields": fields}}
return query



class EBooleanIndex(BaseIndex):
def create_mapping(self, name):
return {"type": "boolean"}
Expand Down
18 changes: 11 additions & 7 deletions src/collective/elasticsearch/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
from typing import Tuple
from zope import schema
from zope.interface import Interface
from zope.schema.vocabulary import SimpleVocabulary
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary


query_types = SimpleVocabulary([
SimpleTerm(value=u'match', title=u'Match (Default)'),
SimpleTerm(value=u'simple_query_string', title=u'Simple Query String')])
query_types = SimpleVocabulary(
[
SimpleTerm(value="match", title="Match (Default)"),
SimpleTerm(value="simple_query_string", title="Simple Query String"),
]
)


class IElasticSearchLayer(Interface):
Expand Down Expand Up @@ -75,10 +79,10 @@ class IElasticSettings(Interface):
)

query_type = schema.Choice(
title=u'Query Type',
title="Query Type",
description="Whether to use match/match_prefix queries or simple_query_string queries. See elastic search docs for more information.",
default=u'match',
vocabulary=query_types
default="match",
vocabulary=query_types,
)

sniff_on_start = schema.Bool(title="Sniff on start", default=False, required=False)
Expand Down
4 changes: 3 additions & 1 deletion src/collective/elasticsearch/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ def _search(self, query, sort=None, **query_params):
"pre_tags": self.highlight_pre_tags.split("\n"),
"post_tags": self.highlight_post_tags.split("\n"),
}
import json; print(json.dumps(body, indent=4))
import json

print(json.dumps(body, indent=4))
return self.connection.search(index=self.index_name, body=body, **query_params)

def search(self, query: dict, factory=None, **query_params) -> LazyMap:
Expand Down

0 comments on commit b05900d

Please sign in to comment.