Skip to content

xmlprc search improvements #3827

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

Merged
merged 1 commit into from
Apr 26, 2018
Merged
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
48 changes: 28 additions & 20 deletions tests/unit/legacy/api/xmlrpc/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ def __getitem__(self, name):
def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.must] == [
{"term": {"name": "foo"}},
{"match": {"name": {"query": "foo", "boost": 10}}},
{
"bool": {
"should": [
{"term": {"summary": "one"}},
{"term": {"summary": "two"}},
{"match":
{"summary": {"query": "one", "boost": 5}}},
{"match":
{"summary": {"query": "two", "boost": 5}}},
],
},
},
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down Expand Up @@ -119,12 +121,14 @@ def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.must] == [
{'bool': {'should': [
{'term': {'summary': 'fix code'}},
{'term': {'summary': 'like this'}}
{'match':
{'summary': {'boost': 5, 'query': 'fix code'}}},
{'match':
{'summary': {'boost': 5, 'query': 'like this'}}}
]}}
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down Expand Up @@ -175,18 +179,20 @@ def __getitem__(self, name):
def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.must] == [
{"term": {"name": "foo"}},
{"match": {"name": {"query": "foo", "boost": 10}}},
{
"bool": {
"should": [
{"term": {"summary": "one"}},
{"term": {"summary": "two"}},
{"match":
{"summary": {"query": "one", "boost": 5}}},
{"match":
{"summary": {"query": "two", "boost": 5}}},
],
},
},
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down Expand Up @@ -238,18 +244,20 @@ def __getitem__(self, name):
def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.should] == [
{"term": {"name": "foo"}},
{"match": {"name": {"query": "foo", "boost": 10}}},
{
"bool": {
"should": [
{"term": {"summary": "one"}},
{"term": {"summary": "two"}},
{"match":
{"summary": {"query": "one", "boost": 5}}},
{"match":
{"summary": {"query": "two", "boost": 5}}},
],
},
},
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down Expand Up @@ -301,11 +309,11 @@ def __getitem__(self, name):
def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.must] == [
{"term": {"name": "foo"}},
{"term": {"version": "1.0"}},
{"match": {"name": {"boost": 10, "query": "foo"}}},
{"match": {"version": {"query": "1.0"}}},
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down Expand Up @@ -357,10 +365,10 @@ def __getitem__(self, name):
def execute(self):
assert self.type == "bool"
assert [q.to_dict() for q in self.must] == [
{"term": {"name": "foo"}},
{"match": {"name": {"query": "foo", "boost": 10}}},
]
assert self.offset is None
assert self.limit == 1000
assert self.limit == 100
assert self.step is None
return [
pretend.stub(
Expand Down
10 changes: 7 additions & 3 deletions warehouse/legacy/api/xmlrpc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from warehouse.packaging.models import (
Role, Project, Release, File, JournalEntry, release_classifiers,
)
from warehouse.search.queries import SEARCH_BOOSTS


_MAX_MULTICALLS = 20
Expand Down Expand Up @@ -107,18 +108,21 @@ def search(request, spec, operator="and"):
for field, value in sorted(spec.items()):
q = None
for item in value:
kw = {"query": item}
if field in SEARCH_BOOSTS:
kw["boost"] = SEARCH_BOOSTS[field]
if q is None:
q = Q("term", **{field: item})
q = Q("match", **{field: kw})
else:
q |= Q("term", **{field: item})
q |= Q("match", **{field: kw})
queries.append(q)

if operator == "and":
query = request.es.query("bool", must=queries)
else:
query = request.es.query("bool", should=queries)

results = query[:1000].execute()
results = query[:100].execute()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@wimglenn wimglenn Jul 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limit could do with some mention in the docs (here or in pip?). Expected results are mysteriously missing from "pip search" if you are searching a common term, and it's not clear why unless digging through the warehouse source code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


request.registry.datadog.histogram('warehouse.xmlrpc.search.results',
len(results))
Expand Down
35 changes: 35 additions & 0 deletions warehouse/search/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SEARCH_FIELDS = [
"author", "author_email", "description", "download_url", "home_page",
"keywords", "license", "maintainer", "maintainer_email", "normalized_name",
"platform", "summary",
]
SEARCH_BOOSTS = {
"name": 10,
"normalized_name": 10,
"description": 5,
"keywords": 5,
"summary": 5,
}
SEARCH_FILTER_ORDER = (
"Framework",
"Topic",
"Development Status",
"License",
"Programming Language",
"Operating System",
"Environment",
"Intended Audience",
"Natural Language",
)
29 changes: 5 additions & 24 deletions warehouse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,15 @@
from warehouse.packaging.models import (
Project, Release, File, release_classifiers,
)
from warehouse.search.queries import (
SEARCH_BOOSTS,
SEARCH_FIELDS,
SEARCH_FILTER_ORDER,
)
from warehouse.utils.row_counter import RowCount
from warehouse.utils.paginate import ElasticsearchPage, paginate_url_factory


SEARCH_FIELDS = [
"author", "author_email", "description", "download_url", "home_page",
"keywords", "license", "maintainer", "maintainer_email", "normalized_name",
"platform", "summary",
]
SEARCH_BOOSTS = {
"normalized_name": 10,
"description": 5,
"keywords": 5,
"summary": 5,
}
SEARCH_FILTER_ORDER = (
"Framework",
"Topic",
"Development Status",
"License",
"Programming Language",
"Operating System",
"Environment",
"Intended Audience",
"Natural Language",
)


# 403, 404, 410, 500,


Expand Down