Улучшена обработка результатов

This commit is contained in:
Jack Stdin 2016-01-17 14:20:38 +03:00
parent 586768b315
commit 4d565e5808
4 changed files with 29 additions and 27 deletions

View File

@ -21,33 +21,39 @@ class SphinxSearch:
self.regression_coef = 0.04
self.db = DBImpl(psycopg2, dbparams)
self.client = sphinxapi.SphinxClient()
self.client.SetServer("localhost", 9312)
self.client.SetLimits(0, 10)
self.client1 = sphinxapi.SphinxClient()
self.client1.SetServer("localhost", 9312)
self.client1.SetLimits(0, 10)
self.client1.SetConnectTimeout(7.0)
self.client_sugg = sphinxapi.SphinxClient()
self.client_sugg.SetServer("localhost", 9312)
self.client_sugg.SetLimits(0, 10)
self.client_sugg.SetConnectTimeout(3.0)
self.client_show = sphinxapi.SphinxClient()
self.client_show.SetServer("localhost", 9312)
self.client_show.SetLimits(0, 10)
self.client_show.SetConnectTimeout(3.0)
def __configure(self, index_name, wlen=None):
if index_name == "idx_fias_sugg":
if wlen:
self.client.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
self.client.SetRankingMode(sphinxapi.SPH_RANK_WORDCOUNT)
self.client.SetFilterRange("len", int(wlen) - self.delta_len, int(wlen) + self.delta_len)
self.client.SetSelect("word, len, @weight+{}-abs(len-{}) AS krank".format(self.delta_len, wlen))
self.client.SetSortMode(sphinxapi.SPH_SORT_EXTENDED, "krank DESC")
self.client_sugg.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
self.client_sugg.SetRankingMode(sphinxapi.SPH_RANK_WORDCOUNT)
self.client_sugg.SetFilterRange("len", int(wlen) - self.delta_len, int(wlen) + self.delta_len)
self.client_sugg.SetSelect("word, len, @weight+{}-abs(len-{}) AS krank".format(self.delta_len, wlen))
self.client_sugg.SetSortMode(sphinxapi.SPH_SORT_EXTENDED, "krank DESC")
else:
self.client_show.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
self.client_show.SetRankingMode(sphinxapi.SPH_RANK_BM25)
def __get_suggest(self, word, rating_limit, count):
word_len = str(len(word) / 2)
trigrammed_word = '"{}"/1'.format(trigram(word))
self.__configure(sphinx_index_sugg, word_len)
result = self.client.Query(trigrammed_word, sphinx_index_sugg)
result = self.client_sugg.Query(trigrammed_word, sphinx_index_sugg)
# Если по данному слову не найдено подсказок (а такое бывает?)
# возвращаем []
if not result['matches']:
return []
@ -90,7 +96,6 @@ class SphinxSearch:
if word_entry.MT_ADD_SOCR:
word_entry.add_variation_socr()
def __get_word_entries(self, words):
for word in words:
if word != '':
@ -98,17 +103,15 @@ class SphinxSearch:
self.__add_word_variations(we)
yield we
def find(self, text):
words = self.__split_phrase(text)
word_entries = self.__get_word_entries(words)
sentence = "{}".format(" MAYBE ".join(x.get_variations() for x in word_entries))
#self.__configure(sphinx_index_addjobj)
self.client1.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
self.client1.SetRankingMode(sphinxapi.SPH_RANK_SPH04)
#self.client1.SetF
rs = self.client1.Query(sentence, sphinx_index_addjobj)
print rs
self.__configure(sphinx_index_addjobj)
rs = self.client_show.Query(sentence, sphinx_index_addjobj)
results = []
for ma in rs['matches']:
print ma['attrs']['fullname'], ma['weight']
print sentence
results.append([ma['attrs']['aoid'], ma['attrs']['fullname'], ma['weight']])
print results

View File

@ -46,7 +46,6 @@ class WordEntry:
self.scname = None
self.ranks = self.__get_ranks()
for x, y in self.match_types.iteritems():
self.__dict__[x] = False
for z in y:
@ -63,8 +62,6 @@ class WordEntry:
self.variations.append(variation_string)
def get_variations(self):
#if len(self.variations) == 1:
# return "\"{}\"".format(self.variations[0])
return "({})".format(" | ".join(self.variations))
def __get_ranks(self):

View File

@ -2,4 +2,6 @@ CREATE INDEX "sphinx_ind_aolevel" ON "ADDROBJ" USING btree ("aolevel");
CREATE INDEX "sphinx_ind_parentguid" ON "ADDROBJ" USING btree ("parentguid");
CREATE INDEX "sphinx_ind_livestatus" ON "ADDROBJ" USING btree ("livestatus");
CREATE INDEX "sphinx_ind_aoguid" ON "ADDROBJ" USING btree ("aoguid");
CREATE INDEX "SOCRBASE_scname_idx" ON "SOCRBASE" USING btree ("scname");
CREATE INDEX "SOCRBASE_socrname_idx" ON "SOCRBASE" USING btree ("socrname");
CREATE INDEX "AOTRIG_word_idx" ON "AOTRIG" USING btree ("word");

View File

@ -54,7 +54,7 @@ def main():
# 4 Debug purposes..
if options.test:
sph = SphinxSearch()
sph.find('#москва$#северное тушино$$$ул#туристская')
sph.find('кридовая паскаул')
if __name__ == '__main__':
main()