Elasticsearch aggregation on results of Suggesters












1















I'm a beginner in Elasticsearch.
I want to aggregate the result of Completion Suggester.



There are multi-indices and multiple tags are added on "tags" field like below.



Documents



GET http://localhost:9200/index1/user-category1/1?pretty

{
"_index": "index1",
"_type": "user-category1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"user_id": 1,
"tags": [
"Apple",
"Orange",
"Peach"
]
}
}

GET http://localhost:9200/index2/user-category2/2?pretty
{
"_index": "index2",
"_type": "user-category2",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"user_id": 2,
"tags": [
"Grape",
"Apple"
]
}
}


And then, I created mappings like this.



Mappings



  "tags": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
},
"suggest": {
"type": "completion"
}
}
}


Query



POST http://localhost:9200/_all/_search?pretty
{
"_source": false,
"suggest": {
"tags_suggest": {
"prefix": "A",
"completion": {
"field": "tags.suggest"
}
}
}
}


Result



{
"took": 5,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": 0,
"hits":
},
"suggest": {
"tags_suggest": [
{
"text": "A",
"offset": 0,
"length": 1,
"options": [
{
"text": "Apple",
"_index": "index1",
"_type": "user-category1",
"_id": "1",
"_score": 1
},
{
"text": "Apple",
"_index": "index2",
"_type": "user-category2",
"_id": "2",
"_score": 1
}
]
}
]
}
}


The query returns the results of suggestions correctly, however, the problem is that I can't get the total count of "Apple"
How can I get the results like below?



"aggregations": {
"keywords": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Apple",
"doc_count": 2
}
]
}
}


I tried the query below, but it doesn't work.



  {
"_source": false,
"suggest": {
"tags_suggest": {
"prefix": "A",
"completion": {
"field": "tags.suggest"
}
}
},
"aggs": {
"tags": {
"terms": {
"field": "tags.keyword"
}
}
}
}









share|improve this question





























    1















    I'm a beginner in Elasticsearch.
    I want to aggregate the result of Completion Suggester.



    There are multi-indices and multiple tags are added on "tags" field like below.



    Documents



    GET http://localhost:9200/index1/user-category1/1?pretty

    {
    "_index": "index1",
    "_type": "user-category1",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
    "user_id": 1,
    "tags": [
    "Apple",
    "Orange",
    "Peach"
    ]
    }
    }

    GET http://localhost:9200/index2/user-category2/2?pretty
    {
    "_index": "index2",
    "_type": "user-category2",
    "_id": "2",
    "_version": 1,
    "found": true,
    "_source": {
    "user_id": 2,
    "tags": [
    "Grape",
    "Apple"
    ]
    }
    }


    And then, I created mappings like this.



    Mappings



      "tags": {
    "type": "text",
    "fields": {
    "raw": {
    "type": "keyword"
    },
    "suggest": {
    "type": "completion"
    }
    }
    }


    Query



    POST http://localhost:9200/_all/_search?pretty
    {
    "_source": false,
    "suggest": {
    "tags_suggest": {
    "prefix": "A",
    "completion": {
    "field": "tags.suggest"
    }
    }
    }
    }


    Result



    {
    "took": 5,
    "timed_out": false,
    "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 0,
    "max_score": 0,
    "hits":
    },
    "suggest": {
    "tags_suggest": [
    {
    "text": "A",
    "offset": 0,
    "length": 1,
    "options": [
    {
    "text": "Apple",
    "_index": "index1",
    "_type": "user-category1",
    "_id": "1",
    "_score": 1
    },
    {
    "text": "Apple",
    "_index": "index2",
    "_type": "user-category2",
    "_id": "2",
    "_score": 1
    }
    ]
    }
    ]
    }
    }


    The query returns the results of suggestions correctly, however, the problem is that I can't get the total count of "Apple"
    How can I get the results like below?



    "aggregations": {
    "keywords": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": "Apple",
    "doc_count": 2
    }
    ]
    }
    }


    I tried the query below, but it doesn't work.



      {
    "_source": false,
    "suggest": {
    "tags_suggest": {
    "prefix": "A",
    "completion": {
    "field": "tags.suggest"
    }
    }
    },
    "aggs": {
    "tags": {
    "terms": {
    "field": "tags.keyword"
    }
    }
    }
    }









    share|improve this question



























      1












      1








      1








      I'm a beginner in Elasticsearch.
      I want to aggregate the result of Completion Suggester.



      There are multi-indices and multiple tags are added on "tags" field like below.



      Documents



      GET http://localhost:9200/index1/user-category1/1?pretty

      {
      "_index": "index1",
      "_type": "user-category1",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
      "user_id": 1,
      "tags": [
      "Apple",
      "Orange",
      "Peach"
      ]
      }
      }

      GET http://localhost:9200/index2/user-category2/2?pretty
      {
      "_index": "index2",
      "_type": "user-category2",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
      "user_id": 2,
      "tags": [
      "Grape",
      "Apple"
      ]
      }
      }


      And then, I created mappings like this.



      Mappings



        "tags": {
      "type": "text",
      "fields": {
      "raw": {
      "type": "keyword"
      },
      "suggest": {
      "type": "completion"
      }
      }
      }


      Query



      POST http://localhost:9200/_all/_search?pretty
      {
      "_source": false,
      "suggest": {
      "tags_suggest": {
      "prefix": "A",
      "completion": {
      "field": "tags.suggest"
      }
      }
      }
      }


      Result



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 2,
      "successful": 2,
      "skipped": 0,
      "failed": 0
      },
      "hits": {
      "total": 0,
      "max_score": 0,
      "hits":
      },
      "suggest": {
      "tags_suggest": [
      {
      "text": "A",
      "offset": 0,
      "length": 1,
      "options": [
      {
      "text": "Apple",
      "_index": "index1",
      "_type": "user-category1",
      "_id": "1",
      "_score": 1
      },
      {
      "text": "Apple",
      "_index": "index2",
      "_type": "user-category2",
      "_id": "2",
      "_score": 1
      }
      ]
      }
      ]
      }
      }


      The query returns the results of suggestions correctly, however, the problem is that I can't get the total count of "Apple"
      How can I get the results like below?



      "aggregations": {
      "keywords": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
      {
      "key": "Apple",
      "doc_count": 2
      }
      ]
      }
      }


      I tried the query below, but it doesn't work.



        {
      "_source": false,
      "suggest": {
      "tags_suggest": {
      "prefix": "A",
      "completion": {
      "field": "tags.suggest"
      }
      }
      },
      "aggs": {
      "tags": {
      "terms": {
      "field": "tags.keyword"
      }
      }
      }
      }









      share|improve this question
















      I'm a beginner in Elasticsearch.
      I want to aggregate the result of Completion Suggester.



      There are multi-indices and multiple tags are added on "tags" field like below.



      Documents



      GET http://localhost:9200/index1/user-category1/1?pretty

      {
      "_index": "index1",
      "_type": "user-category1",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
      "user_id": 1,
      "tags": [
      "Apple",
      "Orange",
      "Peach"
      ]
      }
      }

      GET http://localhost:9200/index2/user-category2/2?pretty
      {
      "_index": "index2",
      "_type": "user-category2",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
      "user_id": 2,
      "tags": [
      "Grape",
      "Apple"
      ]
      }
      }


      And then, I created mappings like this.



      Mappings



        "tags": {
      "type": "text",
      "fields": {
      "raw": {
      "type": "keyword"
      },
      "suggest": {
      "type": "completion"
      }
      }
      }


      Query



      POST http://localhost:9200/_all/_search?pretty
      {
      "_source": false,
      "suggest": {
      "tags_suggest": {
      "prefix": "A",
      "completion": {
      "field": "tags.suggest"
      }
      }
      }
      }


      Result



      {
      "took": 5,
      "timed_out": false,
      "_shards": {
      "total": 2,
      "successful": 2,
      "skipped": 0,
      "failed": 0
      },
      "hits": {
      "total": 0,
      "max_score": 0,
      "hits":
      },
      "suggest": {
      "tags_suggest": [
      {
      "text": "A",
      "offset": 0,
      "length": 1,
      "options": [
      {
      "text": "Apple",
      "_index": "index1",
      "_type": "user-category1",
      "_id": "1",
      "_score": 1
      },
      {
      "text": "Apple",
      "_index": "index2",
      "_type": "user-category2",
      "_id": "2",
      "_score": 1
      }
      ]
      }
      ]
      }
      }


      The query returns the results of suggestions correctly, however, the problem is that I can't get the total count of "Apple"
      How can I get the results like below?



      "aggregations": {
      "keywords": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
      {
      "key": "Apple",
      "doc_count": 2
      }
      ]
      }
      }


      I tried the query below, but it doesn't work.



        {
      "_source": false,
      "suggest": {
      "tags_suggest": {
      "prefix": "A",
      "completion": {
      "field": "tags.suggest"
      }
      }
      },
      "aggs": {
      "tags": {
      "terms": {
      "field": "tags.keyword"
      }
      }
      }
      }






      elasticsearch autosuggest search-suggestion






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 6:24







      Ank

















      asked Nov 22 '18 at 4:00









      AnkAnk

      63




      63
























          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%2f53423687%2felasticsearch-aggregation-on-results-of-suggesters%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%2f53423687%2felasticsearch-aggregation-on-results-of-suggesters%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]