def run(self, query, page=1, default_operator="AND", **kwargs): """Perform search, placing the results in `self.results`, and the total number of results (across all pages) in `self.total`. Chainable.""" search = DSLSearch(using=es7_client(), index=self.get_index()).params( **settings.ES7_SEARCH_PARAMS ) # add the search class' filter search = search.query( self.get_filter(query=query, default_operator=default_operator, **kwargs) ) # add highlights for the search class' highlight_fields search = search.highlight(*self.get_highlight_fields(), **self.get_highlight_options()) # do pagination start = (page - 1) * self.results_per_page search = search[start : start + self.results_per_page] # perform search self.hits = search.execute().hits self.total = self.hits.total.value if self.hits else 0 self.results = [self.make_result(hit) for hit in self.hits] return self
def run(self, key: Union[int, slice] = slice(0, settings.SEARCH_RESULTS_PER_PAGE)): """Perform search, placing the results in `self.results`, and the total number of results (across all pages) in `self.total`. Chainable.""" search = DSLSearch( using=es7_client(), index=self.get_index()).params(**settings.ES7_SEARCH_PARAMS) # add the search class' filter search = search.query(self.get_filter()) # add highlights for the search class' highlight_fields for highlight_field, options in self.get_highlight_fields_options(): search = search.highlight(highlight_field, **options) # slice search search = search[key] # perform search self.hits = search.execute().hits self.last_key = key self.total = self.hits.total.value self.results = [self.make_result(hit) for hit in self.hits] return self
def run(self, query, page=1, default_operator="AND"): """Perform search, placing the results in `self.results`, and the total number of results (across all pages) in `self.total`. Chainable.""" # Default to a dfs query search = DSLSearch( using=es7_client(), index=self.get_index()).params(search_type="dfs_query_then_fetch") # add the search class' filter search = search.query("bool", filter=self.get_filter()) # add query, search over the search class' fields search = search.query( "simple_query_string", query=query, default_operator=default_operator, fields=self.get_fields(), # everything apart from WHITESPACE as that interferes with char mappings # and synonyms with whitespace in them by breaking up the phrase into tokens, # before they have a chance to go through the filter: flags="AND|ESCAPE|FUZZY|NEAR|NOT|OR|PHRASE|PRECEDENCE|PREFIX|SLOP", ) # add highlights for the search class' highlight_fields search = search.highlight( *self.get_highlight_fields(), type="fvh", # order highlighted fragments by their relevance: order="score", # only get one fragment per field: number_of_fragments=1, # split fragments at the end of sentences: boundary_scanner="sentence", # return fragments roughly this size: fragment_size=SNIPPET_LENGTH, # add these tags before/after the highlighted sections: pre_tags=[f"<{HIGHLIGHT_TAG}>"], post_tags=[f"</{HIGHLIGHT_TAG}>"], ) # do pagination start = (page - 1) * self.results_per_page search = search[start:start + self.results_per_page] # perform search self.hits = search.execute().hits self.total = self.hits.total.value if self.hits else 0 self.results = [self.make_result(hit) for hit in self.hits] return self
def run(self, query, page=1, default_operator="AND"): """Perform search, placing the results in `self.results`, and the total number of results (across all pages) in `self.total`. Chainable.""" # Default to a dfs query search = DSLSearch( using=es7_client(), index=self.get_index()).params(search_type="dfs_query_then_fetch") # add the search class' filter search = search.query("bool", filter=self.get_filter()) # add query, search over the search class' fields search = search.query( "simple_query_string", query=query, default_operator=default_operator, fields=self.get_fields(), ) # add highlights for the search class' highlight_fields search = search.highlight( *self.get_highlight_fields(), type="fvh", # order highlighted fragments by their relevance: order="score", # only get one fragment per field: number_of_fragments=1, # split fragments at the end of sentences: boundary_scanner="sentence", # return fragments roughly this size: fragment_size=SNIPPET_LENGTH, # add these tags before/after the highlighted sections: pre_tags=[f"<{HIGHLIGHT_TAG}>"], post_tags=[f"</{HIGHLIGHT_TAG}>"], ) # do pagination start = (page - 1) * self.results_per_page search = search[start:start + self.results_per_page] # perform search self.hits = search.execute().hits self.total = self.hits.total.value if self.hits else 0 self.results = [self.make_result(hit) for hit in self.hits] return self