Search inside array using Elastic Search

Viewed 35

I am using Elastic version 6.8, created one index into whose schema is as follow:

{
"properties": {
    "title": {
        "type": "text",
        "fields": {
            "raw": {
                "type": "keyword"
            }
        }
    },
    "tags": {
        "type": "keyword",
        "fields": {
            "raw": {
                "type": "text"
            }
        }
    }
}}

and I have added following documents into it

    [{
    "title": "one",
    "tags": ["html", "css", "javascript"]
    }, {
    "title": "two",
    "tags": ["java", "jsp", "servlet"]
   }, {
    "title": "three",
    "tags": ["spring", "java"]
   }, {
    "title": "four",
    "tags": ["react", "angular", "javascript"]
   }, {
    "title": "five",
    "tags": ["java"]
   }, {
    "title": "six",
    "tags": []
   }]

now I have more than 10 millions document in elastic search. Now I want to search following cases:

  1. List all tags. with unique result (using skip, limit) skip value change but limit is fixed.so here I want result like

    html, css, javascript, java, jsp, servlet, spring, react, angular

  2. Partil search inside tags, it means if I search using act then it should give result as : react this also using skip limit.

How I can get this using Elastic search query. please help me here?

1 Answers
  1. You can find unique possible value by using term aggregation.
GET yourindex/_search
{
  "size": 0, 
  "aggs": {
    "all_tags": {
      "terms": {
        "field": "tags",
        "size": 100
      }
    }
  }
}
  • "size":100 Get at most 100 unique values. Default is 10. You can increase more but it will include cost. You can check more on doc.

  • For partial search you can use wildcard query OR you can try N-Gram Tokeninzer. Both will allow to do partial search but wildcard query will be costly. You can evaluate according to your use case.

Related