def get_publisher(acc): q = { "query": { "bool": { "must": [{ "term": { "admin.owner.exact": acc.id } }, { "term": { "admin.in_doaj": True } }] } }, "size": 0, "facets": { "publishers": { "terms": { "field": "bibjson.publisher.exact", "size": 1000 } } } } es = Journal.query(q=q) pubs = [ term.get("term") for term in es.get("facets", {}).get( "publishers", {}).get("terms", []) ] if len(pubs) == 0: return None return ", ".join(pubs)
def get_publisher(acc): q = { "query" : { "bool" : { "must" : [ {"term" : {"admin.owner.exact" : acc.id}}, {"term" : {"admin.in_doaj" : True}} ] } }, "size" : 0, "facets" : { "publishers" : { "terms" : { "field" : "bibjson.publisher.exact", "size" : 1000 } } } } es = Journal.query(q=q) pubs = [term.get("term") for term in es.get("facets", {}).get("publishers", {}).get("terms", [])] if len(pubs) == 0: return None return ", ".join(pubs)
def query_es(self): """ Query Elasticsearch for a set of matches for this request. :return: The results of a query through the dao, a JSON object. """ # Copy to the template, which will be populated with terms populated_query = deepcopy(TERMS_SEARCH) # Get all of the attributes with values set. set_attributes = [(x, getattr(self, x)) for x in JOURNAL_SCHEMA_KEYS[:-1] if getattr(self, x)] # If we don't have a genre, guess journal FIXME: is it correct to assume journal? if not self.genre: self.genre = SUPPORTED_GENRES[ 0] # TODO: we may want to handle 404 instead # Set i to use either our mapping for journals or articles i = SUPPORTED_GENRES.index(getattr(self, 'genre').lower()) # Add the attributes to the query for (k, v) in set_attributes: es_term = OPENURL_TO_ES[k][i] if es_term is None: continue else: term = {"term": {es_term: v}} populated_query["query"]["bool"]["must"].append(term) # avoid doing an empty query if len(populated_query["query"]["bool"]["must"]) == 0: app.logger.debug("No valid search terms in OpenURL object") return None # Return the results of the query if i == 0: app.logger.debug("OpenURL query to journal: " + json.dumps(populated_query)) return Journal.query(q=populated_query) elif i == 1: app.logger.debug("OpenURL query to article: " + json.dumps(populated_query)) return Article.query(q=populated_query)
def query_es(self): """ Query Elasticsearch for a set of matches for this request. :return: The results of a query through the dao, a JSON object. """ # Copy to the template, which will be populated with terms populated_query = deepcopy(TERMS_SEARCH) # Get all of the attributes with values set. set_attributes = [(x, getattr(self, x)) for x in JOURNAL_SCHEMA_KEYS[:-1] if getattr(self, x)] # If we don't have a genre, guess journal FIXME: is it correct to assume journal? if not self.genre: self.genre = SUPPORTED_GENRES[0] # TODO: we may want to handle 404 instead # Set i to use either our mapping for journals or articles i = SUPPORTED_GENRES.index(getattr(self, 'genre').lower()) # Add the attributes to the query for (k, v) in set_attributes: es_term = OPENURL_TO_ES[k][i] if es_term is None: continue else: term = {"term": {es_term: v}} populated_query["query"]["bool"]["must"].append(term) # avoid doing an empty query if len(populated_query["query"]["bool"]["must"]) == 0: app.logger.debug("No valid search terms in OpenURL object") return None # Return the results of the query if i == 0: app.logger.debug("OpenURL query to journal: " + json.dumps(populated_query)) return Journal.query(q=populated_query) elif i == 1: app.logger.debug("OpenURL query to article: " + json.dumps(populated_query)) return Article.query(q=populated_query)