Skip to content

Commit

Permalink
Merge 188c3a3 into 857c4e7
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbachmann committed Dec 5, 2017
2 parents 857c4e7 + 188c3a3 commit 64c2d06
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions scripts/empty-cores.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo "Emptying cores: $@"

for core in "$@"; do
curl "http://solr:8983/solr/$core/update?stream.body=<delete><query>*:*</query></delete>&commit=true"
done
File renamed without changes.
5 changes: 2 additions & 3 deletions sleuth_backend/solr/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def insert_documents(self, core_name, docs):
'''
if core_name not in self.cores:
raise ValueError('No Solr core with the name "{}" was found'.format(core_name))
print('Inserting '+str(len(docs))+' items into core '+core_name)
print('Inserting {} items into core {}'.format(str(len(docs)), core_name))
return self.cores[core_name].add(docs)

def insert_queued(self):
Expand Down Expand Up @@ -158,8 +158,7 @@ def optimize(self, core_name=None):
raise ValueError('No Solr core with the name "{}" was found'.format(core_name))
self.cores[core_name].optimize()
else:
for core in self.cores:
self.cores[core].optimize()
[self.cores[core].optimize() for core in self.cores]

def _get_url(self, url, params):
'''
Expand Down
9 changes: 4 additions & 5 deletions sleuth_backend/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from sleuth.settings import HAYSTACK_CONNECTIONS

SOLR = solr.SolrConnection(HAYSTACK_CONNECTIONS['default']['URL'])
DEFAULT_CORE = "test"

def cores(request):
'''
Expand Down Expand Up @@ -42,7 +41,7 @@ def search(request):
Example Usage:
http:// ... /api/search/?q=hello&core=genericPage&return=children
Example Response Body:
Example Response Body:
{
"data": [
{
Expand Down Expand Up @@ -120,11 +119,11 @@ def search(request):
message=query_response['error']['msg']+" on core "+core_to_search
)
return HttpResponse(sleuth_error.json(), status=query_response['error']['code'])
# Attach type to response and flatten single-item list fields

# Attach type to response and flatten single-item list fields (except for links)
query_response['type'] = core_to_search
for doc in query_response['response']['docs']:
utils.flatten_doc(doc, return_fields)
utils.flatten_doc(doc, return_fields, ['links'])

responses['data'].append(query_response)

Expand Down
19 changes: 13 additions & 6 deletions sleuth_backend/views/views_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,29 @@ def build_return_fields(fields):

return return_fields + ',' + ','.join(fields_list)

def flatten_doc(doc, return_fields):
def flatten_doc(doc, return_fields, exceptions=None):
'''
Flattens single-item list fields returned by Solr
@param doc a dictionary containing the contents of a document
@param return_fields a string of comma-separated field names
@param exceptions an array of names of fields that shouldn't be flattened
Flattens single-item list fields in genericPage returned by Solr ignoring
any fields that appear in the given list of exceptions.
'''
for f in return_fields.split(","):
if f in doc:
doc[f] = doc[f][0] if len(doc[f]) == 1 else doc[f]
else:
if exceptions is not None and f in exceptions:
continue
elif f not in doc:
doc[f] = ''
else:
doc[f] = doc[f][0] if len(doc[f]) == 1 else doc[f]
return doc

def build_search_query(core, query_str, base_kwargs):
'''
Builds a search query and sets parameters that is most likely to
return the best results for the given core using the given user query.
See https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
for more information about Apache Lucene query syntax.
'''
Expand Down

0 comments on commit 64c2d06

Please sign in to comment.