I'm working on a QA (survey) application. I have implemented Elastic Search in the application. I want to filter users based on their answers. Suppose, there are questions:
- Do you have a passport? Yes/No
- Do you have a driving license? Yes/No
Now I want to fetch those users who answered Yes
to both questions.
My answer document looks like this.
@registry.register_document
class AnswerDocument(Document):
user = fields.ObjectField(properties={
'id': fields.IntegerField(),
'name': fields.TextField(),
})
question = fields.ObjectField(properties={
'id': fields.IntegerField(),
'name': fields.TextField(),
'description': fields.TextField(),
})
class Index:
name = 'answers'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0,
}
class Django:
model = Answer
fields = [
'id',
'yes_no_ans',
]
I have built a query expression but it's not working as expected.
expression = (Q('bool', should=[
Q('match', question__name='passport'),
Q('match', yes_no_ans='yes'),
], minimum_should_match=2)
)
&
(Q('bool',
should=[
Q('match', question__name='driving license'),
Q('match', yes_no_ans='yes'),
],
minimum_should_match=2))
response = AnswerDocument.search().query(expression).execute()
Can anyone help me with this? Thank you.