Filter context within query context


(George Schneeloch) #1

I have an index containing documents which have the object_type field set to comment or post. For comment I want to search only the text field, for post I want to search both text and post_title. Essentially I'd like to query something like this: (doc.object_type = "post" and (doc.text matching text or doc.post_title matching text)) or (doc.object_type = "comment" and doc.text matching text)

When I run the query below I'm seeing posts come up in the results despite the comment filter. Can someone explain what the inner filter clause does here? Is it possible to nest a filter inside a should?

{
  "from": 0,
  "size": 5,
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "channel_name": "jeanne4"
              }
            }
          ]
        }
      },
      "should": [
        {
          "bool": {
            "filter": {
              "term": {
                "object_type": "comment"
              }
            },
            "must": {
              "match": {
                "text": "amtrak"
              }
            }
          }
        }
      ]
    }
  }
}

(Jaspreet Singh) #2

What is the purpose of this filter?

 "filter": {
                "bool": {
                    "should": [
                        {
                            "term": {
                                "channel_name": "jeanne4"
                            }
                        }
                    ]
                }
            }

Cos this is basically bringing in anything that is in channel_name = "jeanne4" including comments, posts etc. You are getting a union of the filter and should clauses. Your post does not mention anything about channels so it is hard to tell what you are trying to do. Perhaps you are limiting all searching to a particular channel, in which case this should come in a must clause.
Just this part should get you only comments that have the text "amtrack".

{
    "bool": {
        "filter": {
            "term": {
                "object_type": "comment"
            }
        },
        "must": {
            "match": {
                "text": "amtrak"
            }
        }
    }
}

(George Schneeloch) #3

Hi Jaspreet, sorry I was not more detailed in my post. My intention was to have the filter on channel_name be an intersection, not a union. That looks like the root of my problem, thanks for your help.


(Jaspreet Singh) #4

Yea sure - no worries. This can be very tricky. Let me know if you have further questions.


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.