ElasticSearch subquery with filter

OK, it probably won't be too hard for one of you ElasticSearch super experts. I have this subquery and I want the subquery to be filtered on a non-nested field (status). I do not know where to put the filter. I tried to put it in the query (below), but that does not give me the correct results. Can you help me?

{
  "aggs": {
    "status": {
      "terms": {
        "field": "status",
        "size": 0
      }
    }
  },
  "filter": {
    "nested": {
      "path": "participants",
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "user_id": 1
              }
            },
            {
              "term": {
                "archived": false
              }
            },
            {
              "term": {
                "has_unread": true
              }
            }
          ]
        }
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "status": 8
              }
            }
          ]
        }
      }
    }
  }
}
+4
source share
1 answer

There are several moving parts here:

  • filter, , - post post, . , , 0,90 , Elasticsearch 5.0.

    , , , , , , .

    • post_filter.
  • nested , .

    {
      "term": {
        "user_id": 1
      }
    }
    

    :

    {
      "term": {
        "participants.user_id": 1
      }
    }
    

    .

  • , , status 8, .

  • size of 0 , . , .

( , , , , ):

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must" : {
            "nested" : {
              "path" : "participants",
              "filter": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "participants.user_id": 1
                      }
                    },
                    {
                      "term": {
                        "participants.archived": false
                      }
                    },
                    {
                      "term": {
                        "participants.has_unread": true
                      }
                    }
                  ]
                }
              }
            }
          },
          "must_not": {
            "term": {
              "status": 8
            }
          }
        }
      }
    }
  },
  "aggs": {
    "status": {
      "terms": {
        "field": "status",
        "size": 0
      }
    }
  }
}

. "must_not" . , , . , , .

+13

All Articles