from mysolr import Solr

from magpie.settings import settings


_solr = None


def open_solr_connection(core_name):
    global _solr
    if not _solr:
        url = '{}/{}'.format(settings.SOLR_URL, core_name)
        _solr = Solr(url)
    return _solr
from mysolr import Solr

from magpie.settings import settings


_solr = None


def open_solr_connection(core_name):
    global _solr
    if not _solr:
        url = '{}/{}'.format(settings.SOLR_URL, core_name)
        _solr = Solr(url)
    return _solr


def escape_solr_query(query):
    """
    Escape special chars for Solr queries.
    """
    chars = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?',
             ':', '/', ' ']
    for char in chars:
        query = query.replace(char, '\{}'.format(char))

    return query
Add method to escape special chars in Solr queries
Add method to escape special chars in Solr queries

  from mysolr import Solr
  
  from magpie.settings import settings
  
  
  _solr = None
  
  
  def open_solr_connection(core_name):
      global _solr
      if not _solr:
          url = '{}/{}'.format(settings.SOLR_URL, core_name)
          _solr = Solr(url)
      return _solr
+ 
+ 
+ def escape_solr_query(query):
+     """
+     Escape special chars for Solr queries.
+     """
+     chars = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?',
+              ':', '/', ' ']
+     for char in chars:
+         query = query.replace(char, '\{}'.format(char))
+ 
+     return query
Add method to escape special chars in Solr queries
## Code Before:
from mysolr import Solr

from magpie.settings import settings


_solr = None


def open_solr_connection(core_name):
    global _solr
    if not _solr:
        url = '{}/{}'.format(settings.SOLR_URL, core_name)
        _solr = Solr(url)
    return _solr
## Instruction:
Add method to escape special chars in Solr queries
## Code After:
from mysolr import Solr

from magpie.settings import settings


_solr = None


def open_solr_connection(core_name):
    global _solr
    if not _solr:
        url = '{}/{}'.format(settings.SOLR_URL, core_name)
        _solr = Solr(url)
    return _solr


def escape_solr_query(query):
    """
    Escape special chars for Solr queries.
    """
    chars = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?',
             ':', '/', ' ']
    for char in chars:
        query = query.replace(char, '\{}'.format(char))

    return query