1
votes

I'm trying to exclude duplicated documents which have the same slug parameters to do it I use aggs in ElasticSearch (version 2.4). I use - this query:

 {
  "fields":[
    "id",
    "score"],
  "size":0,
  "query":{
    "function_score":{
      "query":{
        "bool":{
          "should":[
            {
              "match":{
                "main_headline.en":{
                  "query":"headline_for_search"
                }
              }
            },
            {
              "match":{
                "body.en":"body for search"
              }
            }],
          "must_not":{
            "term":{
              "id":75333
            }
          },
          "filter":[
            {
              "term":{
                "status":3
              }
            },
            [
              {
                "term":{
                  "sites":6
                }
              }]]
        }
      },
      "functions":[
        {
          "gauss":{
            "published_at":{
              "scale":"140w",
              "decay":0.3
            }
          }
        }
      ]
    },
    "aggs":{
      "postslug":{
        "terms":{
          "field":"slug",
          "order":{
            "top_score":"desc"
          }
        },
        "aggs":{
          "grouppost":{
            "top_hits": {
              "_source": {
                "include": [
                  "id",
                  "slug",
                ]
              },
              "size" : 10
            }
          }
        }
      }
    }
  }
}

When I run it I get error

failed to parse search source. expected field name but got [START_OBJECT] I can`t figure out where is a mistake.

Without section aggs all works fine (except present duplicates)

1

1 Answers

1
votes

I see one issue which relates to the fact that in the source filtering section include should read includes. Also, the aggs section is not at the right location, you have it in the query section, and it should be at the top-level:

{
  "fields": [
    "id",
    "score"
  ],
  "size": 0,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "main_headline.en": {
                  "query": "headline_for_search"
                }
              }
            },
            {
              "match": {
                "body.en": "body for search"
              }
            }
          ],
          "must_not": {
            "term": {
              "id": 75333
            }
          },
          "filter": [
            {
              "term": {
                "status": 3
              }
            },
            [
              {
                "term": {
                  "sites": 6
                }
              }
            ]
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "published_at": {
              "scale": "140w",
              "decay": 0.3
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "postslug": {
      "terms": {
        "field": "slug",
        "order": {
          "top_score": "desc"
        }
      },
      "aggs": {
        "grouppost": {
          "top_hits": {
            "_source": {
              "includes": [
                "id",
                "slug"
              ]
            },
            "size": 10
          }
        }
      }
    }
  }
}