Filtering searches with Elasticsearch












0















My project is a search tool which searches by questions and/or responses. I want it to also be able to filter the searches by e.g. topics, clients. Each response is mapped to a question, topic and client with a foreign key.



I am able to search by questions and responses but when I add in the filter topics (drop down list), it returns 0 results.



models.py:



class Topic(models.Model):
Definition = models.TextField(default='Definition')
Name = models.TextField(default='Name')
class Meta:
ordering = ['Name']
def __str__(self):
return self.Name

class Question(models.Model):
Statement = models.TextField()
def __str__(self):
return self.Statement

class Response(models.Model):
Question = models.ForeignKey(Question, on_delete=models.CASCADE)
Topic = models.ForeignKey(Topic, default=13, on_delete=models.CASCADE)
Response = models.TextField()
Client = models.ForeignKey(ClientDetail, default=8, on_delete=models.CASCADE)
Planit_location = models.ForeignKey(Planit_location, default=1, on_delete=models.CASCADE)
Date_added = models.DateField(default=datetime.date.today)

def __str__(self):
return self.Response


documents.py:



class ResponseDocument20(DocType):
Question = fields.NestedField(properties={
'Statement': fields.TextField(),
'pk': fields.IntegerField(),
}, include_in_root=True)
class Meta:
model = Response

fields = [
'Response'
]
related_models = [Question]

def get_instances_from_related(self, related_instance):
if isinstance(related_instance, Question):
return related_instance.response_set.all()


views.py:



if q:#ES
cleanQ = stopwords.strip_stopwords(q)
if (queryR == "questions"):
if (queryT != "empty"):
responses = ResponseDocument20.search().filter(Q_ES("match", Question__Statement=cleanQ.lower())&Q_ES("match", Topic__Name=queryT)).extra(size=10000)
else:
responses = ResponseDocument20.search().query("match", Question__Statement=cleanQ.lower()).extra(size=10000) #ES


I assumed all I had to add was Topic__Name=queryT to filter the search by topic and it would give me results that matched the search input AND topic.










share|improve this question





























    0















    My project is a search tool which searches by questions and/or responses. I want it to also be able to filter the searches by e.g. topics, clients. Each response is mapped to a question, topic and client with a foreign key.



    I am able to search by questions and responses but when I add in the filter topics (drop down list), it returns 0 results.



    models.py:



    class Topic(models.Model):
    Definition = models.TextField(default='Definition')
    Name = models.TextField(default='Name')
    class Meta:
    ordering = ['Name']
    def __str__(self):
    return self.Name

    class Question(models.Model):
    Statement = models.TextField()
    def __str__(self):
    return self.Statement

    class Response(models.Model):
    Question = models.ForeignKey(Question, on_delete=models.CASCADE)
    Topic = models.ForeignKey(Topic, default=13, on_delete=models.CASCADE)
    Response = models.TextField()
    Client = models.ForeignKey(ClientDetail, default=8, on_delete=models.CASCADE)
    Planit_location = models.ForeignKey(Planit_location, default=1, on_delete=models.CASCADE)
    Date_added = models.DateField(default=datetime.date.today)

    def __str__(self):
    return self.Response


    documents.py:



    class ResponseDocument20(DocType):
    Question = fields.NestedField(properties={
    'Statement': fields.TextField(),
    'pk': fields.IntegerField(),
    }, include_in_root=True)
    class Meta:
    model = Response

    fields = [
    'Response'
    ]
    related_models = [Question]

    def get_instances_from_related(self, related_instance):
    if isinstance(related_instance, Question):
    return related_instance.response_set.all()


    views.py:



    if q:#ES
    cleanQ = stopwords.strip_stopwords(q)
    if (queryR == "questions"):
    if (queryT != "empty"):
    responses = ResponseDocument20.search().filter(Q_ES("match", Question__Statement=cleanQ.lower())&Q_ES("match", Topic__Name=queryT)).extra(size=10000)
    else:
    responses = ResponseDocument20.search().query("match", Question__Statement=cleanQ.lower()).extra(size=10000) #ES


    I assumed all I had to add was Topic__Name=queryT to filter the search by topic and it would give me results that matched the search input AND topic.










    share|improve this question



























      0












      0








      0








      My project is a search tool which searches by questions and/or responses. I want it to also be able to filter the searches by e.g. topics, clients. Each response is mapped to a question, topic and client with a foreign key.



      I am able to search by questions and responses but when I add in the filter topics (drop down list), it returns 0 results.



      models.py:



      class Topic(models.Model):
      Definition = models.TextField(default='Definition')
      Name = models.TextField(default='Name')
      class Meta:
      ordering = ['Name']
      def __str__(self):
      return self.Name

      class Question(models.Model):
      Statement = models.TextField()
      def __str__(self):
      return self.Statement

      class Response(models.Model):
      Question = models.ForeignKey(Question, on_delete=models.CASCADE)
      Topic = models.ForeignKey(Topic, default=13, on_delete=models.CASCADE)
      Response = models.TextField()
      Client = models.ForeignKey(ClientDetail, default=8, on_delete=models.CASCADE)
      Planit_location = models.ForeignKey(Planit_location, default=1, on_delete=models.CASCADE)
      Date_added = models.DateField(default=datetime.date.today)

      def __str__(self):
      return self.Response


      documents.py:



      class ResponseDocument20(DocType):
      Question = fields.NestedField(properties={
      'Statement': fields.TextField(),
      'pk': fields.IntegerField(),
      }, include_in_root=True)
      class Meta:
      model = Response

      fields = [
      'Response'
      ]
      related_models = [Question]

      def get_instances_from_related(self, related_instance):
      if isinstance(related_instance, Question):
      return related_instance.response_set.all()


      views.py:



      if q:#ES
      cleanQ = stopwords.strip_stopwords(q)
      if (queryR == "questions"):
      if (queryT != "empty"):
      responses = ResponseDocument20.search().filter(Q_ES("match", Question__Statement=cleanQ.lower())&Q_ES("match", Topic__Name=queryT)).extra(size=10000)
      else:
      responses = ResponseDocument20.search().query("match", Question__Statement=cleanQ.lower()).extra(size=10000) #ES


      I assumed all I had to add was Topic__Name=queryT to filter the search by topic and it would give me results that matched the search input AND topic.










      share|improve this question
















      My project is a search tool which searches by questions and/or responses. I want it to also be able to filter the searches by e.g. topics, clients. Each response is mapped to a question, topic and client with a foreign key.



      I am able to search by questions and responses but when I add in the filter topics (drop down list), it returns 0 results.



      models.py:



      class Topic(models.Model):
      Definition = models.TextField(default='Definition')
      Name = models.TextField(default='Name')
      class Meta:
      ordering = ['Name']
      def __str__(self):
      return self.Name

      class Question(models.Model):
      Statement = models.TextField()
      def __str__(self):
      return self.Statement

      class Response(models.Model):
      Question = models.ForeignKey(Question, on_delete=models.CASCADE)
      Topic = models.ForeignKey(Topic, default=13, on_delete=models.CASCADE)
      Response = models.TextField()
      Client = models.ForeignKey(ClientDetail, default=8, on_delete=models.CASCADE)
      Planit_location = models.ForeignKey(Planit_location, default=1, on_delete=models.CASCADE)
      Date_added = models.DateField(default=datetime.date.today)

      def __str__(self):
      return self.Response


      documents.py:



      class ResponseDocument20(DocType):
      Question = fields.NestedField(properties={
      'Statement': fields.TextField(),
      'pk': fields.IntegerField(),
      }, include_in_root=True)
      class Meta:
      model = Response

      fields = [
      'Response'
      ]
      related_models = [Question]

      def get_instances_from_related(self, related_instance):
      if isinstance(related_instance, Question):
      return related_instance.response_set.all()


      views.py:



      if q:#ES
      cleanQ = stopwords.strip_stopwords(q)
      if (queryR == "questions"):
      if (queryT != "empty"):
      responses = ResponseDocument20.search().filter(Q_ES("match", Question__Statement=cleanQ.lower())&Q_ES("match", Topic__Name=queryT)).extra(size=10000)
      else:
      responses = ResponseDocument20.search().query("match", Question__Statement=cleanQ.lower()).extra(size=10000) #ES


      I assumed all I had to add was Topic__Name=queryT to filter the search by topic and it would give me results that matched the search input AND topic.







      python django elasticsearch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 14:29







      Lloyd

















      asked Nov 14 '18 at 13:22









      LloydLloyd

      816




      816
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301248%2ffiltering-searches-with-elasticsearch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301248%2ffiltering-searches-with-elasticsearch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues