Skip to content

Instantly share code, notes, and snippets.

@mattmontgomery
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save mattmontgomery/33b1a6f335546c43a692 to your computer and use it in GitHub Desktop.

Select an option

Save mattmontgomery/33b1a6f335546c43a692 to your computer and use it in GitHub Desktop.

Aggregations

Can replace faceting (facets api deprecated in ES). Lots of options.

  • min aggregation
  • max aggregation
  • sum aggregation
  • avg aggregation
  • stats aggregation
  • extended stats aggregation
  • value count aggregation
  • percentiles aggregation
  • percentile ranks aggregation
  • cardinality aggregation
  • geo bounds aggregation
  • top hits aggregation
  • scripted metric aggregation
  • global aggregation
  • filter aggregation
  • filters aggregation
  • missing aggregation
  • nested aggregation
  • reverse nested aggregation
  • children aggregation
  • terms aggregation
  • significant terms aggregation
  • range aggregation
  • date range aggregation
  • ipv4 range aggregation
  • histogram aggregation
  • date histogram aggregation
  • geo distance aggregation
  • geohash grid aggregation

Queries

curl -XGET "http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search?search_type=count" -d '{
  "query": {
      "term": {
          "content_data.title": "football"
      }
  },
  "aggregations": {
    "my_agg": {
      "terms": {
        "field": "ext_publication"
      }
    }
  }
}
'

Search type: Count

Counts are approximate.

curl -XGET 'http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search?search_type=count' -d '{
  "aggregations": {
    "my_agg": {
      "terms": {
        "field": "content_data.sites"
      }
    }
  }
}
'
curl -XGET 'http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search?search_type=count' -d '{
  "query": {
      "term": {
          "content_data.sites": "cougarfan"
      }
  },
  "aggregations": {
    "my_agg": {
      "terms": {
        "field": "content_data.title"
      }
    }
  }
}
'

Response

{
    "took": 197,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "hits": {
        "total": 2136801,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "my_agg": {
            "buckets": [
                {
                    "key": "dnews",
                    "doc_count": 18519
                },
                {
                    "key": "national",
                    "doc_count": 3157
                },
                {
                    "key": "cougarfan",
                    "doc_count": 2151
                },
                {
                    "key": "common",
                    "doc_count": 16
                },
                {
                    "key": "ddm",
                    "doc_count": 5
                },
                {
                    "key": "utah",
                    "doc_count": 3
                },
                {
                    "key": "ksl",
                    "doc_count": 1
                }
            ]
        }
    }
}

Suggestions

In order to use some suggesters (completion), you have to specify it in the mapping.

You could solve with queries, but suggestions are faster. They can be used in addition to search.

Suggesters

  • term: word alternatives on a per-token basis
  • phrase: select entire corrected phrases
  • completion (requires mapping changes)

Stats API

Index stats

http://cms-elastic-test.deseretdigital.com:9200/_stats http://cms-elastic-test.deseretdigital.com:9200/contentindex/_stats http://cms-elastic-test.deseretdigital.com:9200/contentindex/_stats/docs http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_stats

  • Document counts
  • Index disk usage
  • Flush / merge / etc stats
  • Average query & fetch time
  • Everything else you ever wanted, maybe

Cluster stats

http://cms-elastic-test.deseretdigital.com:9200/_cluster/stats

Node stats

http://cms-elastic-test.deseretdigital.com:9200/_nodes/stats

Fuzziness, relevancy, boosting, etc.

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/controlling-relevance.html

Fuzzy query

Simple

curl -XGET "http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search" -d '{
  "query": {
      "fuzzy": {
          "content_data.title": "futebol"
      }
  }
}
'

More options

curl -XGET "http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search" -d '{
  "query": {
      "fuzzy": {
          "timestamp_modified": {
              "value": "2014-10-10T00:00:00",
              "fuzziness": "1d"
          }
      }
  }
}
'

Query boosting & negative boosting

curl -XGET "http://cms-elastic-test.deseretdigital.com:9200/contentindex/ContentProfile/_search" -d '{
  "query": {
      "boosting": {
          "positive": {
            "match": {
              "content_data.sites": "cougarfan"
            }
          },
          "negative": {
              "match": {
                "title": "BYU"
              }
          },
          "negative_boost": 0.1
      }
  }
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment