def test_solr_request_add_sort(self): req = SearchRequest("foo") self.assertEqual(req.params.sort, None) req.add_sort("bar", "desc") self.assertEqual(req.params.sort, "bar desc") req.add_sort("baz", "asc") self.assertEqual(req.params.sort, "bar desc,baz asc")
def test_02_solr_request_http_method(self): req = SearchRequest("foo") prepared = req.prepare("http://example.com/select") self.assertEqual(prepared.method, 'GET') prepared = req.prepare("http://example.com/select", method='POST') self.assertEqual(prepared.method, 'POST') with self.app.test_request_context(): solr.request_http_method = 'POST' with fake_solr_http_response(): resp = solr.query(**{'q': "black holes"}) self.assertEqual(resp.request.prepared.method, 'POST') solr.request_http_method = 'GET'
def test_solr_request_add_filter_query(self): req = SearchRequest("foo") req.add_filter_query("bibstem:ApJ") self.assertEqual(req.params.fq, ['bibstem:ApJ']) req.add_filter_query("author:Kurtz,M") self.assertEqual(req.params.fq, ['bibstem:ApJ', 'author:Kurtz,M']) self.assertEqual(req.get_filter_queries(), ['bibstem:ApJ', 'author:Kurtz,M'])
def test_solr_request_add_facet_output_key(self): req = SearchRequest("foo") req.add_facet("author", output_key="authorz") self.assertEqual(req.params['facet.field'], ["{!ex=dt key=authorz}author"]) req = SearchRequest("foo") req.add_facet("title", limit=10, output_key="titlez") self.assertEqual(req.params['facet.field'], ["{!ex=dt key=titlez}title"]) self.assertEqual(req.params['f.title.facet.limit'], 10)
def _create_search_request(self): q = "identifier:%s OR doi:%s" % (self.record_id, self.record_id) req = SearchRequest(q, rows=1) if self.form.fl.data: fields = list(set(self.form.fl.data.split(',') + config.SOLR_SEARCH_REQUIRED_FIELDS)) req.set_fields(fields) else: req.set_fields(self.user.get_allowed_fields()) if len(self.form.hl.data): for hl in self.form.hl.data: hl = hl.split(':') req.add_highlight(*hl) if self.form.hlq.data: req.set_hlq(self.form.hlq.data) return req
def test_solr_request_add_highlight(self): req = SearchRequest("foo") self.assertNotIn('hl', req.params) self.assertFalse(req.highlights_on()) req.add_highlight("abstract") self.assertTrue(req.highlights_on()) self.assertEqual(req.params['hl'], 'true') self.assertEqual(req.params['hl.fl'], 'abstract') self.assertEqual(req.get_highlights(), [('abstract', None, None)]) req.add_highlight("full") self.assertEqual(req.params['hl.fl'], 'abstract,full') self.assertEqual(req.get_highlights(), [('abstract', None, None), ('full', None, None)]) req.add_highlight('full', 2) self.assertEqual(req.get_highlights(), [('abstract', None, None), ('full', 2, None)]) req.add_highlight('foo') req.add_highlight('bar') self.assertEqual(req.params['hl.fl'], 'abstract,full,foo,bar') req.add_highlight('baz', 3) req.add_highlight('fez', 3) self.assertEqual(req.params['f.baz.hl.snippets'], 3) self.assertEqual(req.params['f.fez.hl.snippets'], 3) self.assertNotIn('f.baz.hl.fragsize', req.params) req.add_highlight("blah", 3, 5000) self.assertEqual(req.params['f.blah.hl.fragsize'], 5000)
def test_solr_request_add_facet(self): req = SearchRequest("foo") self.assertFalse(req.facets_on()) self.assertNotIn('facet', req.params) req.add_facet('author') self.assertTrue(req.facets_on()) self.assertEqual(req.get_facets(), [('author', None, None, None, None)]) self.assertEqual(req.params['facet'], 'true') self.assertEqual(req.params['facet.field'], ['author']) req.add_facet('bibstem') self.assertEqual(req.get_facets(), [('author', None, None, None, None), ('bibstem', None, None, None, None)]) self.assertEqual(req.params['facet.field'], ['author', 'bibstem']) req.add_facet('keyword', 10) self.assertEqual(req.get_facets(), [('author', None, None, None, None), ('bibstem', None, None, None, None), ('keyword', 10, None, None, None)]) self.assertEqual(req.params['facet.field'], ['author', 'bibstem', 'keyword']) self.assertIn('f.keyword.facet.limit', req.params) self.assertEqual(req.params['f.keyword.facet.limit'], 10) req.add_facet('author', 10, 5) self.assertEqual(req.get_facets(), [('author', 10, 5, None, None), ('bibstem', None, None, None, None), ('keyword', 10, None, None, None)]) self.assertEqual(req.params['facet.field'], ['author', 'bibstem', 'keyword']) self.assertIn('f.author.facet.limit', req.params) self.assertIn('f.author.facet.mincount', req.params) self.assertEqual(req.params['f.author.facet.limit'], 10) self.assertEqual(req.params['f.author.facet.mincount'], 5)
def test_solr_request_constructor_overrides(self): req = SearchRequest("foo", foo='fez', bar="baz") self.assertEqual(req.params['foo'], 'fez') self.assertEqual(req.params['bar'], 'baz')
def test_solr_request_set_query_fields(self): req = SearchRequest("foo") self.assertIsNone(req.params.qf) req.set_query_fields("bar baz^1.2") self.assertEqual(req.params.qf, "bar baz^1.2")
def test_solr_request_setters(self): req = SearchRequest("foo") req.set_fields(['foo','bar']) expected = ['foo','bar'] self.assertEqual(set(req.get_fields()), set(expected)) req.set_rows(100) self.assertEqual(req.params.rows, 100) req.set_start(10) self.assertEqual(req.params.start, 10) req.set_sort("bar", "desc") self.assertEqual(req.params.sort, "bar desc") self.assertEqual(req.get_sort(), [('bar','desc')]) req.set_sort("baz", "asc") self.assertEqual(req.params.sort, "baz asc") self.assertEqual(req.get_sort(), [('baz','asc')])
def test_solr_request_setters(self): req = SearchRequest("foo") req.set_fields(['foo', 'bar']) expected = ['foo', 'bar'] self.assertEqual(set(req.get_fields()), set(expected)) req.set_rows(100) self.assertEqual(req.params.rows, 100) req.set_start(10) self.assertEqual(req.params.start, 10) req.set_sort("bar", "desc") self.assertEqual(req.params.sort, "bar desc") self.assertEqual(req.get_sort(), [('bar', 'desc')]) req.set_sort("baz", "asc") self.assertEqual(req.params.sort, "baz asc") self.assertEqual(req.get_sort(), [('baz', 'asc')])
def test_solr_request_defaults(self): req = SearchRequest("foo") self.assertIn('q', req.params) self.assertEqual(req.params['q'], "foo") self.assertEqual(req.params['wt'], "json")
def test_solr_request_add_facet_prefix(self): req = SearchRequest("foo") req.add_facet_prefix("author", "bar") self.assertIn("f.author.facet.prefix", req.params) self.assertEqual(req.params['f.author.facet.prefix'], "bar")
def _create_search_request(self): req = SearchRequest(self.form.q.data) if self.form.fl.data: fields = list(set(self.form.fl.data.split(',') + config.SOLR_SEARCH_REQUIRED_FIELDS)) req.set_fields(fields) else: req.set_fields(self.user.get_allowed_fields()) if self.form.rows.data: req.set_rows(self.form.rows.data) else: req.set_rows(config.SEARCH_DEFAULT_ROWS) if self.form.start.data: req.set_start(self.form.start.data) if self.form.sort.data: (sort, direction) = self.form.sort.data.split() sort_field = config.SOLR_SORT_OPTIONS[sort] req.set_sort(sort_field, direction) else: req.set_sort(*config.API_SOLR_DEFAULT_SORT) if len(self.form.facet.data): for facet in self.form.facet.data: facet = facet.split(':') api_facet_name = facet[0] solr_field_name = config.API_SOLR_FACET_FIELDS[api_facet_name] if api_facet_name != solr_field_name: # translate api facet name to solr field name in request *and* response # see http://wiki.apache.org/solr/SimpleFacetParameters#key_:_Changing_the_output_key output_key = api_facet_name facet[0] = solr_field_name else: output_key = None req.add_facet(*facet, output_key=output_key) if len(self.form.hl.data): for hl in self.form.hl.data: req.add_highlight(hl.split(':')) if len(self.form.filter.data): for fltr in self.form.filter.data: req.add_filter(fltr) if self.form.hlq.data: req.set_hlq(self.form.hlq.data) return req
def _create_search_request(self): req = SearchRequest(self.form.q.data) if self.form.fl.data: fields = list(set(self.form.fl.data.split(',') + config.SOLR_SEARCH_REQUIRED_FIELDS)) else: fields = self.user.get_allowed_fields() req.set_fields(fields) if self.form.rows.data: req.set_rows(self.form.rows.data) else: req.set_rows(config.SEARCH_DEFAULT_ROWS) if self.form.start.data: req.set_start(self.form.start.data) if self.form.sort.data: sort_tokens = re.split('[\s,]+', self.form.sort.data) # tupleize the list into field,direction pairs sort_tokens = zip(*[iter(sort_tokens)] * 2) for sort, direction in sort_tokens: sort_field = config.SEARCH_SORT_OPTIONS_MAP[sort][0] req.add_sort(sort_field, direction) else: for field, direction in config.API_SOLR_DEFAULT_SORT: req.add_sort(field, direction) if len(self.form.facet.data): for facet in self.form.facet.data: facet = facet.split(':') api_facet_name = facet[0] solr_field_name = config.API_SOLR_FACET_FIELDS[api_facet_name] if api_facet_name != solr_field_name: # translate api facet name to solr field name in request *and* response # see http://wiki.apache.org/solr/SimpleFacetParameters#key_:_Changing_the_output_key output_key = api_facet_name facet[0] = solr_field_name else: output_key = None req.add_facet(*facet, output_key=output_key) if len(self.form.hl.data): for hl in self.form.hl.data: if ':' in hl: req.add_highlight(*hl.split(':')) else: req.add_highlight(hl) if len(self.form.filter.data): for fltr in self.form.filter.data: req.add_filter_query(fltr) if self.form.hlq.data: req.set_hlq(self.form.hlq.data) return req