Get ElasticSearch suggestion based on filtered data in Django using autocomplete

17 Views Asked by At

`I am using django-elasticsearch-dsl for integrating elastic search. I have an API that returns name, email, phonenumber, location, etc., suggestions using elastic search suggest feature.

It's working as expected but the issue is I can't apply a specific filter. For example, I want to get suggestions for the particular section of students only.

#Document @INDEX.doc_type class StudentDocument(DocType):

name = StringField(analyzer=html_strip,
                         fields={
                             'raw': KeywordField(),
                             'suggest': fields.CompletionField(),})
email = StringField(analyzer=html_strip,
                        fields={
                            'raw': KeywordField(),
                            'suggest': fields.CompletionField(),
                        })

#View class StudentSuggestions(APIView): def get(self, request, search_ele): url = host + "/student/_search"
search_ele = str(search_ele) search_ele = re.sub(r'[^a-zA-Z0-9]+', '', search_ele).lower()

        data = {
            # "query" : {
            #         "match": {
            #         "screening": True
            #         }
            #     },
                "suggest": {
                    "Names": {
                        "prefix": search_ele,
                        "completion": {
                            "field": "name.suggest"
                            # "contexts": {
                            #     "screening": True
                            # }
                        },
                        # "options":
                        # "term" : {
                        #     "screening" : True
                        # }
                    },
                  
                    "Tags": {
                    "prefix": search_ele,
                    "completion": {
                        "field": "tags.suggest"
                    }
                    },
                   
                    "Emails": {
                    "prefix": search_ele,
                    "completion": {
                        "field": "email.suggest"
                    }
                    },
            }}}
        result = requests.post(url, json=data, headers=header, auth=auth, verify=False)
        print("Results: ", result.status_code)
        searchresult = result.json()

`

0

There are 0 best solutions below