美文网首页
Elasticsearch:处理 AttributeError:

Elasticsearch:处理 AttributeError:

作者: dex0423 | 来源:发表于2020-07-17 19:03 被阅读0次

    1. 环境

    • windows 10;
    • elasticsearch 7.8.0;
    • Django

    2. 问题

    • 使用 elasticsearch + Django 搭建搜索引擎,在获取 suggest 时遇到 AttributeError: 'Response' object has no attribute 'my_suggest' 报错,
    • 问题代码如下:
    from SEARCH.models import ArticleType  # 注意:ArticleType 是在另一个文件单独定义的!
    from django.http import HttpResponse
    from elasticsearch import Elasticsearch
    from datetime import datetime
    
    client = Elasticsearch(hosts=["127.0.0.1"])
    
    s = ArticleType.search()
    s = s.suggest(
        'my_suggest',
        "xx",
        completion={
            "field": "suggest",
            "fuzzy": {
                "fuzziness": 2
            },
            "size": 10
        }
    )
    suggestions = s.execute()
    
    • 报错如下:

    KeyError: 'my_suggest'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
    File "C:\Users\Administrator\Desktop\E_engine\E_search\SEARCH\views.py", line 36, in get
    for match in suggestions.my_suggest[0].options:
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch_dsl\utils.py", line 130, in getattr
    '{!r} object has no attribute {!r}'.format(self.class.name, attr_name))
    AttributeError: 'Response' object has no attribute 'my_suggest'

    3. 解决

    • 原因
      elasticsearch 版本更新导致用法变更,问题代码是旧的写法,需要将 s.execute_suggest() 变更为 s.execute().to_dict()
    • 更新代码
    from django.http import HttpResponse
    from elasticsearch import Elasticsearch
    from datetime import datetime
    
    client = Elasticsearch(hosts=["127.0.0.1"])
    
    s = ArticleType.search()
    s = s.suggest(
        'my_suggest',
        "九州",
        completion={
            "field": "suggest",
            "fuzzy": {
                "fuzziness": 2
            },
            "size": 10
        }
    )
    # 调用 execute_suggest 方法
    # suggestions = s.execute()
    suggestions = s.execute().to_dict()    # 此处将 s.execute_suggest()  变更为  s.execute().to_dict()
    print("#" * 50)
    print(suggestions)
    print("#" * 50)
    
    • 打印结果如下,问题解决:
    image.png

    相关文章

      网友评论

          本文标题:Elasticsearch:处理 AttributeError:

          本文链接:https://www.haomeiwen.com/subject/oornhktx.html