python操作solr

发布时间:2019-09-07 07:57:48编辑:auto阅读(1917)

    solr接收http请求,所以使用requests库操作solr就可以

    添加

        data = {"add": {"doc": params, "commitWithin": 1000}}
        headers = {"Content-type": "application/json"}
        re = requests.post(url=url, data=json.dumps(data),
                           headers=headers)

    查询

        params = list()
        params.append(('q', "id:" + id))
        response = requests.post(url=url, data=params)

    很多时候请求文本存在特殊字符,所以需要转义(或者用括号括起来)

        def escape_query_chars(s):
        escape_chars = set(
                ('\\', '+', '-', '!', '(', ')', ':', '^', '[', ']', '\"', '{', '}', '~', '*', '?', '|', '&', '', '/', ' ','\t'))
            arr = [c for c in s if (c not in escape_chars)]
            return "".join(arr).replace("\xe3\x80\x80", "")

关键字