0

My PySolr codes is below

filter_queries = []
        facet_fields   = []

        for key, value in request.GET._iterlists():
            if(key != 'Rows' and key != 'Sort' and key != 'Facet' and key != 'FacetFields' ):
                filter_queries.append(key + ':' + request.GET[key])
            else:
                if(key == 'Rows'):
                    rows = request.GET['Rows']

                if(key == 'Sort'):
                    sort = request.GET['Sort']

                if(key == 'Facet'):
                    facet = request.GET['Facet']

                if(key == 'FacetFields'):
                    facet_fields = request.GET['FacetFields'].split(",")

                if(key == 'Start'):
                    start = request.GET['Start']

 params = {
            'fq': filter_queries,
            'facet': facet,
            'facet.field': facet_fields,
            'rows': rows,
            'sort': sort,
            'start':start,
        }

try:
            results = solr.search(q='*', **params)
            documents = json.dumps(results, default=lambda o: o.__dict__)
        except Exception as e:
            # handles SOLR invalid queries
            documents = json.dumps({'error': 1, 'reason': str(e)})

        return HttpResponse(documents)

When I am passing ?CityId = 3156 its working fine, but now I need to pass two values for CityId(eg like solr query url ?q=CityId:3156+OR+CityId:2278) how to achieve this in PySolr ?

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45

2 Answers2

1

Im using AJAX for the requests, but the syntax is the same in all APIs for Solr. if you want to make multiple queries on a single field you should use parenthesis like so:

CityId:(3156+OR+2278)

That should do the trick.

Jorge Lazo
  • 388
  • 7
  • 18
1

It is ok for you to just write query like

CityId:(3156+OR+2278)

Or, you can just write your query normaly, set q.op parameter in your params

 query = 'CityId: 3156 2278'
 params = {
        'fq': ...,
        'facet': ...,
        'facet.field':...,
        'rows': ...,
        'sort': ...,
        'start':...,
        'q.op': 'OR'
    }
yomin
  • 551
  • 5
  • 14