Pages

Showing posts with label ElasticSearch. Show all posts
Showing posts with label ElasticSearch. Show all posts

Tuesday, 11 February 2014

TERM Facets with Script Field

TERM Facets with Script Field

A script that provides the actual terms that will be processed for a given doc. A script_field (or script which will be used when no field or fields are provided) can be set to provide it.

Here is an example 1:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true
{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "script_field": "_source.name"
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 0,
      "terms": [
        {
          "term": "lumia 720",
          "count": 1
        },
        {
          "term": "lumia 625",
          "count": 1
        },
        {
          "term": "lumia 520",
          "count": 1
        },
        {
          "term": "lumia 510",
          "count": 1
        },
        {
          "term": "canvas",
          "count": 1
        }
      ]
    }
  }
}

Here is an example 2:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true
{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "script_field": "_source['name']"
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 0,
      "terms": [
        {
          "term": "lumia 720",
          "count": 1
        },
        {
          "term": "lumia 625",
          "count": 1
        },
        {
          "term": "lumia 520",
          "count": 1
        },
        {
          "term": "lumia 510",
          "count": 1
        },
        {
          "term": "canvas",
          "count": 1
        }
      ]
    }
  }
}


TERM Facets with Multi Fields

TERM Facets with Multi Fields

The term facet can be executed against more than one field, returning the aggregation result across those fields.

Here is an example:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true
{
    "query": {
        "query_string": {
            "query": "can*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                 "fields" : ["_type", "name"]
            }
        }
    }
}

Results:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 2,
      "other": 0,
      "terms": [
        {
          "term": "micromax",
          "count": 1
        },
        {
          "term": "canvas",
          "count": 1
        }
      ]
    }
  }
}


TERM Facets with Term Scripts

TERM Facets with Term Scripts

Allow to define a script for terms facet to process the actual term that will be used in the term facet collection, and also optionally control its inclusion or not.
The script can either return a boolean value, with true to include it in the facet collection, and false to exclude it from the facet collection.
Another option is for the script to return a string controlling the term that will be used to count against. The script execution will include the term variable which is the current field term used.

Here is an example for script return a string:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true
{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "field": "_type",
                "script": "term + ' script'"
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 0,
      "terms": [
        {
          "term": "nokia script",
          "count": 4
        },
        {
          "term": "micromax script",
          "count": 1
        }
      ]
    }
  }
}

Here is an example for script return a Boolean:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true
{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "field": "_type",
                "script": "term == 'nokia' ? true : false"
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 1,
      "terms": [
        {
          "term": "nokia",
          "count": 4
        }
      ]
    }
  }
}


TERM Facets with Excluding Terms

TERM Facets with Excluding Terms

Allow to specify a set of terms that should be excluded from the terms facet.

Here is an example:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true

{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "field": "_type",
                "exclude": [
                    "nokia"
                ]
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 4,
      "terms": [
        {
          "term": "micromax",
          "count": 1
        }
      ]
    }
  }
}

TERM Facets with Ordering

TERM Facets with Ordering

Allow to control the ordering of the terms facets, to be ordered by count, term, reverse_count or reverse_term. The default is count.

Here is an example:

POST Method:

http://localhost:9200/mobiles/_search?pretty=true

{
    "query": {
        "query_string": {
            "query": "*a*"
        }
    },
    "facets": {
        "type": {
            "terms": {
                "field": "_type",
 "order" : "term",
                "all_terms": true
            }
        }
    }
}

Results:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "lumia 510"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "lumia 520"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lumia 625"
        }
      },
      {
        "_index": "mobiles",
        "_type": "nokia",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "lumia 720"
        }
      },
      {
        "_index": "mobiles",
        "_type": "micromax",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "canvas"
        }
      }
    ]
  },
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 0,
      "terms": [
        {
          "term": "micromax",
          "count": 1
        },
        {
          "term": "nokia",
          "count": 4
        },
        {
          "term": "samsung",
          "count": 0
        }
      ]
    }
  }
}