0
votes

I have query POST query

localhost:8000/_search/?pretty=true&q=Abbeline+Black

{
  "aggs": {
    "dedup": {
      "terms": {
        "field": "image_url_hash",
        "size":0
      },
      "aggs": {
        "dedup_docs": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

Response:

{
  "took": 84,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {...},
  "aggregations": {
    "dedup": {
      "doc_count_error_upper_bound": 10,
      "sum_other_doc_count": 2213,
      "buckets": [
        {
          "key": "14fe6e82a38688aa13dd8592ab12e8c0",
          "doc_count": 8,
          "dedup_docs": {
            "hits": {
              "total": 8,
              "max_score": 0.012580806,
              "hits": [
                {
                  "_index": "snder.co",
                  "_type": "product_feed",
                  "_id": "AVGucg-_uLMPZm4CUkuv",
                  "_score": 0.012580806,
                  "_source": {
                    "title": "Chinese Laundry Stilo Pointy Toe Pump Black 8.5",
                    "id": "17357703440022",
                    "image_url_hash": "14fe6e82a38688aa13dd8592ab12e8c0"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "91fe5757349f5de7a5dcf716ebd6f3e3",
          "doc_count": 7,
          "dedup_docs": {
            "hits": {
              "total": 7,
              "max_score": 0.012580806,
              "hits": [
                {
                  "_index": "snder.co",
                  "_type": "product_feed",
                  "_id": "AVGucjvluLMPZm4CUkyC",
                  "_score": 0.012580806,
                  "_source": {
                    "title": "Sorel Joan of Arctic Waterproof Boots Black 6",
                    "id": "17212803440017",
                    "image_url_hash": "91fe5757349f5de7a5dcf716ebd6f3e3",

                  }
                }
              ]
            }
          }
        },
        {
          "key": "81c48b614facf034653417498b8a1659",
          "doc_count": 6,
          "dedup_docs": {
            "hits": {
              "total": 6,
              "max_score": 0.012577212,
              "hits": [
                {
                  "_index": "snder.co",
                  "_type": "product_feed",
                  "_id": "AVGucn3o7bmz9_YSCrEA",
                  "_score": 0.012577212,
                  "_source": {
                    "title": "Chinese Laundry Over The Knee Tie Back Boots Black 6",
                    "id": "17795103440017",
                    "image_url_hash": "81c48b614facf034653417498b8a1659",
                  }
                }
              ]
            }
          }
        },
        {
          "key": "d16c929f3490620e95ad0ef8e8d3117a",
          "doc_count": 6,
          "dedup_docs": {
            "hits": {
              "total": 6,
              "max_score": 0.018849576,
              "hits": [
                {
                  "_index": "snder.co",
                  "_type": "product_feed",
                  "_id": "AVGucedY7bmz9_YSCq4l",
                  "_score": 0.018849576,
                  "_source": {
                    "title": "Abbeline Tank Black S",
                    "id": "17480407090041",
                    "image_url_hash": "d16c929f3490620e95ad0ef8e8d3117a",

                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Right now it sorts by number of doc_count. I wanted to sort the buckets by _score.

I changed this request to localhost:8000/_search/?pretty=true&q=Abbeline+Black

{
  "aggs": {
    "dedup": {
      "terms": {
        "field": "image_url_hash",
        "order": {
          "top_hit": "desc"
        }
      },
      "aggs": {
        "dedup_docs": {
          "top_hits": {}
        },
        "top_hit" : {
            "max": {
            "script": "_score"
          }
        }

      }
    }
  }
}

And I keep getting this error

{ "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[OiTaTwM1S_CyfvTX1bDNrg][snder.co][0]: RemoteTransportException[[Burstarr][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[snder.co][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"aggs\": {\n \"dedup\": {\n \"terms\": {\n \"field\": \"image_url_hash\",\n \"order\": {\n \"top_hit\": \"desc\"\n }\n },\n \"aggs\": {\n
\"dedup_docs\": {\n \"top_hits\": {}\n },\n
\"top_hit\" : {\n \"max\": {\n \"script\": \"_score\"\n }\n }\n \n }\n }\n }\n}]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[OiTaTwM1S_CyfvTX1bDNrg][snder.co][1]: RemoteTransportException[[Burstarr][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[snder.co][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"aggs\": {\n \"dedup\": {\n \"terms\": {\n \"field\": \"image_url_hash\",\n \"order\": {\n \"top_hit\": \"desc\"\n }\n },\n \"aggs\": {\n
\"dedup_docs\": {\n \"top_hits\": {}\n },\n
\"top_hit\" : {\n \"max\": {\n \"script\": \"_score\"\n }\n }\n \n }\n }\n }\n}]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[OiTaTwM1S_CyfvTX1bDNrg][southmoonunder.com][2]: RemoteTransportException[[Burstarr][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[snder.co][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"aggs\": {\n \"dedup\": {\n \"terms\": {\n \"field\": \"image_url_hash\",\n \"order\": {\n \"top_hit\": \"desc\"\n }\n },\n \"aggs\": {\n
\"dedup_docs\": {\n \"top_hits\": {}\n },\n
\"top_hit\" : {\n \"max\": {\n \"script\": \"_score\"\n }\n }\n \n }\n }\n }\n}]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[OiTaTwM1S_CyfvTX1bDNrg][southmoonunder.com][3]: RemoteTransportException[[Burstarr][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[snder.co][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"aggs\": {\n \"dedup\": {\n \"terms\": {\n \"field\": \"image_url_hash\",\n \"order\": {\n \"top_hit\": \"desc\"\n }\n },\n \"aggs\": {\n
\"dedup_docs\": {\n \"top_hits\": {}\n },\n
\"top_hit\" : {\n \"max\": {\n \"script\": \"_score\"\n }\n }\n \n }\n }\n }\n}]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[OiTaTwM1S_CyfvTX1bDNrg][southmoonunder.com][4]: RemoteTransportException[[Burstarr][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[snder.co][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"aggs\": {\n \"dedup\": {\n \"terms\": {\n \"field\": \"image_url_hash\",\n \"order\": {\n \"top_hit\": \"desc\"\n }\n },\n \"aggs\": {\n
\"dedup_docs\": {\n \"top_hits\": {}\n },\n
\"top_hit\" : {\n \"max\": {\n \"script\": \"_score\"\n }\n }\n \n }\n }\n }\n}]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }]", "status": 400 }

Can some one help me how do I sort by max_score instead of doc_counts?

1

1 Answers

0
votes

According to the error dynamic scripting for [groovy] disabled you simply need to enable dynamic scripting in your elasticsearch.yml config file (on all your nodes).

If you are on ES 1.6+,:

script.inline: on

If you are on an earlier version:

script.disable_dynamic: false