Last active
June 2, 2017 17:46
-
-
Save wshirey/ac79fca664b834c49f689d7635261bad to your computer and use it in GitHub Desktop.
elasticsearch demo snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How many of each program types are available? | |
GET programs-han/_search | |
{ | |
"size": 0, | |
"aggs": { | |
"types": { | |
"terms": { | |
"field": "type.keyword", | |
"size": 5 | |
} | |
} | |
} | |
} | |
# How many of each program types are available for the 5 clients with the most programs? | |
GET programs-han/_search | |
{ | |
"size": 0, | |
"aggs": { | |
"clients": { | |
"terms": { | |
"field": "client_id", | |
"size": 5 | |
}, | |
"aggs": { | |
"types": { | |
"terms": { | |
"field": "type.keyword" | |
} | |
} | |
} | |
} | |
} | |
} | |
# How many different clients are there | |
GET programs-han/_search | |
{ | |
"size": 0, | |
"aggs": { | |
"clients": { | |
"cardinality": { | |
"field": "client_id" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Nest; | |
namespace Daxko.Ops.IFX.Elasticsearch.Documents.Offerings | |
{ | |
[ElasticsearchType(Name = "offering", IdProperty = "elasticsearch_id")] | |
public class OfferingDocument : IDocument | |
{ | |
[Text(Ignore = true)] | |
public string elasticsearch_id { get; set; } | |
[Keyword] | |
public string client_id { get; set; } | |
public DateTime timestamp { get; set; } | |
[Keyword] | |
public string id { get; set; } | |
[Keyword] | |
public string ops_stack { get; set; } | |
[Text(Ignore = true)] | |
public string primary_key_id { get; set; } | |
[Text(Analyzer = "english")] | |
public string name { get; set; } | |
[Text(Analyzer = "english")] | |
public string description { get; set; } | |
public string type { get; set; } | |
public Program program { get; set; } | |
[Nested(IncludeInRoot = true)] | |
public List<Location> locations { get; set; } | |
[Nested(IncludeInRoot = true)] | |
public List<Category> categories { get; set; } | |
[Nested(IncludeInRoot = true)] | |
public List<RegistrationDate> registration_dates { get; set; } | |
[Nested(IncludeInRoot = true)] | |
public List<Date> dates { get; set; } | |
public Restrictions restrictions { get; set; } | |
public DaysOfWeek days_offered { get; set; } | |
[Nested(IncludeInRoot = true)] | |
public List<TimeRange> times { get; set; } | |
public bool? is_active { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE test | |
# index document | |
PUT test/email/1 | |
{ | |
"message": "Hello world!" | |
} | |
# get document you just added | |
GET test/email/1 | |
# see how dynamic mapping is set | |
GET test/_mapping | |
# add new field | |
PUT test/email/1 | |
{ | |
"delivered": false | |
} | |
# see how mapping has changed | |
GET test/_mapping | |
# specify mapping | |
PUT test2 | |
{ | |
"mappings": { | |
"email": { | |
"properties": { | |
"message": { | |
"type": "text" | |
} | |
} | |
} | |
} | |
} | |
PUT test2/text_message/1 | |
{ | |
"number": "eight six seven five three oh nine" | |
} | |
PUT test2/text_message/1 | |
{ | |
"number": 8675309 | |
} | |
GET test2/text_message/1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET _analyze | |
{ | |
"analyzer": "standard", | |
"text": "The QUICK brown Foxes jumps over the lazy dog." | |
} | |
GET _analyze | |
{ | |
"analyzer": "english", | |
"text": "The QUICK brown Foxes jumps over the lazy dog." | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET programs-han/_search | |
{ | |
"size": 1, | |
"query": { | |
"match": { | |
"description": "swim" | |
} | |
}, | |
"highlight": { | |
"fields": { | |
"description": {} | |
} | |
} | |
} | |
GET programs-han/_search | |
{ | |
"size": 1, | |
"query": { | |
"match": { | |
"description": "swim" | |
} | |
}, | |
"highlight": { | |
"fields": { | |
"description": { | |
"fragment_size": 30, | |
"number_of_fragments": 2 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get mapping for index | |
GET programs-han/_mapping | |
# get templates for cluster | |
GET _template | |
# get all documents in cluster | |
GET _search | |
# get all documents in specific index | |
GET programs-han/_search | |
# get all documents in specific index and type | |
GET programs-han/offering/_search |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE my-index | |
PUT /my-index | |
{ | |
"mappings": { | |
"doctype": { | |
"properties": { | |
"message": { | |
"type": "text" | |
} | |
} | |
}, | |
"queries": { | |
"properties": { | |
"query": { | |
"type": "percolator" | |
}, | |
"contact_info": { | |
"type": "keyword" | |
} | |
} | |
} | |
} | |
} | |
# will let me know if document.message matches "bonsai tree" | |
PUT /my-index/queries/1?refresh | |
{ | |
"query" : { | |
"match" : { | |
"message" : "bonsai tree" | |
} | |
}, | |
"contact_info": "wshirey@daxko.com" | |
} | |
GET /my-index/_search | |
{ | |
"query" : { | |
"percolate" : { | |
"field" : "query", | |
"document_type" : "doctype", | |
"document" : { | |
"message" : "A new bonsai tree in the office" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET _template | |
GET programs-han/_mapping | |
GET programs-han/_aliases | |
GET programs-han/_search | |
GET programs-han/_search | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"client_id": { | |
"value": "2014" | |
} | |
} | |
} | |
] | |
} | |
}, | |
"aggs": { | |
"types": { | |
"terms": { | |
"field": "type.keyword", | |
"size": 10 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Filter - Show me programs that have the program description exactly equal to "Swim Safe Swim Lessons" | |
GET programs-han/_search | |
{ | |
"_source": "program.description", | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"program.description.keyword": { | |
"value": "Swim Safe Swim Lessons" | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
# Query - Show me programs that have the word swim in the description? | |
GET programs-han/_search | |
{ | |
"_source": "description", | |
"query": { | |
"match": { | |
"description": "swim" | |
} | |
} | |
} | |
# Fuzzy - Show me programs that have the word slim or similar terms in the program description | |
GET programs-han/_search | |
{ | |
"_source": "program.description", | |
"query": { | |
"fuzzy": { | |
"program.description": { | |
"fuzziness": 1, | |
"value": "slim" | |
} | |
} | |
} | |
} | |
# Boosting - Show me programs that have the word swim, and show me on | |
GET programs-han/_search | |
{ | |
"_source": ["description", "name", "program.name", "program.description"], | |
"query": { | |
"multi_match": { | |
"query": "swimming", | |
"fields": [ | |
"name^10", | |
"description^5", | |
"program.name^3", | |
"program.description^2" | |
] | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment