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

hybrid search #1854

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions mpcontribs-api/mpcontribs/api/contributions/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,32 @@ class Contributions(DynamicDocument):
ReferenceField("Attachments", null=True), default=list, max_length=10
)
notebook = ReferenceField("Notebooks")
atlas = AtlasManager("formula_autocomplete")
atlas = AtlasManager(
"formula_autocomplete",
definition={
"analyzer": "lucene.whitespace",
"searchAnalyzer": "lucene.whitespace",
"mappings": {
"dynamic": False,
"fields": {
"formula": {"type": "string"},
"identifier": {"type": "string"},
"is_public": {"type": "boolean"},
"project": [{"type": "stringFacet"}, {"type": "string"}],
},
},
"storedSource": {
"include": [
"formula",
"identifier",
"is_public",
"last_modified",
"needs_build",
"project",
]
},
},
)
meta = {
"collection": "contributions",
"indexes": [
Expand All @@ -183,39 +208,8 @@ class Contributions(DynamicDocument):

@queryset_manager
def objects(doc_cls, queryset):
return queryset.no_dereference().only(
"project",
"identifier",
"formula",
"is_public",
"last_modified",
"needs_build",
)

@classmethod
def atlas_filter(cls, term):
try:
comp = Composition(term)
except Exception:
raise ValueError(f"{term} is not a valid composition")

try:
for element in comp.elements:
Element(element)
except Exception:
raise ValueError(f"{element} not a valid element")

ind_str = []

if len(comp) == 1:
d = comp.get_integer_formula_and_factor()
ind_str.append(d[0] + str(int(d[1])) if d[1] != 1 else d[0])
else:
for i, j in comp.reduced_composition.items():
ind_str.append(i.name + str(int(j)) if j != 1 else i.name)

final_terms = ["".join(entry) for entry in permutations(ind_str)]
return AtlasQ(formula=final_terms[0]) # TODO formula__in=final_terms
only = doc_cls.atlas.definition["storedSource"]["include"]
return queryset.no_dereference().only(*only)

@classmethod
def post_init(cls, sender, document, **kwargs):
Expand Down
50 changes: 0 additions & 50 deletions mpcontribs-api/mpcontribs/api/contributions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,53 +169,3 @@ def has_add_permission(self, req, obj):
raise Unauthorized(f"{obj.identifier} already added for {obj.project.id}")

return True


@contributions.route("/search")
def search():
formula = request.args.get("formula")
if not formula:
abort(404, description="Missing formula param.")

try:
comp = Composition(formula)
except (CompositionError, ValueError):
abort(400, description="Invalid formula provided.")

ind_str = []

if len(comp) == 1:
d = comp.get_integer_formula_and_factor()
ind_str.append(d[0] + str(int(d[1])) if d[1] != 1 else d[0])
else:
for i, j in comp.reduced_composition.items():
ind_str.append(i.name + str(int(j)) if j != 1 else i.name)

final_terms = ["".join(entry) for entry in permutations(ind_str)]
limit = request.args.get("limit", ContributionsResource.default_limit)

pipeline = [
{
"$search": {
"index": "formula_autocomplete",
"text": {"path": "formula", "query": final_terms},
}
},
{"$project": {"formula": 1, "length": {"$strLenCP": "$formula"}, "project": 1}},
{"$match": {"length": {"$gte": len(final_terms[0])}}},
{"$limit": limit},
{"$sort": {"length": 1}},
]

results = []

for contrib in Contributions.objects().aggregate(pipeline):
results.append(
{
"id": str(contrib["_id"]),
"formula": contrib["formula"],
"project": contrib["project"],
}
)

return jsonify(results)
35 changes: 25 additions & 10 deletions mpcontribs-api/mpcontribs/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,18 @@ def has_read_permission(self, request, qs):
return qs.none()
else:
names = None
if q and "project" in q and "$in" in q["project"]:
names = q.pop("project").pop("$in")
if hasattr(qs._query_obj, "children"):
children = deepcopy(qs._query_obj.children)
else:
children = [deepcopy(qs._query_obj)]

qs._query_obj = Q()
for node in children:
for field, value in node.query.items():
if field == "project__in":
names = value
else:
qs = qs.filter(**{field: value})

qfilter = self.get_projects_filter(
username, groups, filter_names=names
Expand Down Expand Up @@ -610,15 +620,20 @@ def has_read_permission(self, request, qs):
qfilter = self.get_projects_filter(username, groups)
component = component[:-1] if component == "notebooks" else component
qfilter &= Q(**{f"{component}__in": ids})
contribs = Contributions.objects(qfilter).only(component).limit(len(ids))
contribs = (
Contributions.objects(qfilter).only(component).limit(len(ids))
)
# return new queryset using "ids__in"
readable_ids = [
getattr(contrib, component).id for contrib in contribs
] if component == "notebook" else [
dbref.id for contrib in contribs
for dbref in getattr(contrib, component)
if dbref.id in ids
]
readable_ids = (
[getattr(contrib, component).id for contrib in contribs]
if component == "notebook"
else [
dbref.id
for contrib in contribs
for dbref in getattr(contrib, component)
if dbref.id in ids
]
)
if not readable_ids:
return qs.none()

Expand Down
Loading