예제 #1
0
 def construct_index(cls, opts, bases):
     i = None
     if opts is None:
         # Inherit Index from base classes
         for b in bases:
             if getattr(b, "_index", DEFAULT_INDEX) is not DEFAULT_INDEX:
                 parent_index = b._index
                 i = Index(
                     parent_index._name,
                     doc_type=parent_index._mapping.doc_type,
                     using=parent_index._using,
                 )
                 i._settings = parent_index._settings.copy()
                 i._aliases = parent_index._aliases.copy()
                 i._analysis = parent_index._analysis.copy()
                 i._doc_types = parent_index._doc_types[:]
                 break
     if i is None:
         i = Index(
             getattr(opts, "name", "*"),
             doc_type=getattr(opts, "doc_type", "doc"),
             using=getattr(opts, "using", "default"),
         )
     i.settings(**getattr(opts, "settings", {}))
     i.aliases(**getattr(opts, "aliases", {}))
     for a in getattr(opts, "analyzers", ()):
         i.analyzer(a)
     return i
예제 #2
0
def gen_suggests(index, info_tuple):
    # 根据字符串生成搜索建议数组
    used_words = set()
    suggests = []
    for text, weight in info_tuple:
        if text:
            # 调用es的analyze接口分析字符串
            ik_analyzer = CustomAnalyzer("ik_max_word", filter=["lowercase"])
            my_analyzer = analyzer('my_analyzer',
                                   tokenizer=tokenizer('trigram',
                                                       'nGram',
                                                       min_gram=3,
                                                       max_gram=3),
                                   filter=['lowercase'])
            i = Index(index)
            i._analysis = ik_analyzer
            # i.analyzer(analyzer=ik_analyzer)

            # i.analyzer.default.type: "ik_max_word"
            a = i.analyze(params={'filter': ["lowercase"]}, body=text)

            # i.analyzer(analyzer = "ik_max_word")

            words = es.indices.analyze(index=index,
                                       params={'filter': ["lowercase"]},
                                       body=text)
            anylyzed_words = set(
                [r["token"] for r in words["tokens"] if len(r["token"]) > 1])
            new_words = anylyzed_words - used_words
        else:
            new_words = set()

        if new_words:
            suggests.append({"input": list(new_words), "weight": weight})

    return suggests