Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
courses:cs397:winter2017:elasticsearch [2017/03/27 17:12] – [General Info about elasticsearch instance] admincourses:cs397:winter2017:elasticsearch [2017/04/16 19:26] (current) – [Inserting documents to an index] admin
Line 2: Line 2:
  
 The following assumes that elasticsearch is running on the localhost on port 9200. The following assumes that elasticsearch is running on the localhost on port 9200.
 +
 +How do I learn more?  https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html
  
 ===== General Info about elasticsearch instance ===== ===== General Info about elasticsearch instance =====
Line 14: Line 16:
   curl -XGET 'localhost:9200/_cat/indices?v&pretty’   curl -XGET 'localhost:9200/_cat/indices?v&pretty’
  
-===== Searching =====+===== Searching Data ===== 
 + 
 + 
 +==== Viewing Data ==== 
 + 
 +This will give you, by default, the first 20 results as prettified JSON 
 + 
 +  curl –XGET ‘localhost:9200/index/_search?pretty’ 
 + 
 +==== Filtering Results ==== 
 + 
 +Only get results that match certain criteria:  
 + 
 +  curl -XGET 'localhost:9200/index/_search?pretty' -H 'Content-Type: application/json' –d’{"query": { "match": { "field": "value" } } }' 
 + 
 + 
 +  curl -XGET 'localhost:9200/index/_search?pretty' -H 'Content-Type: application/json' –d’{ "query":
 +    "bool":
 +      "must":
 +        { "match": { "address": "mill" } }, 
 +        { "match": { "address": "lane" } } 
 +        ] 
 +      } 
 +    } 
 +  }' 
 + 
 +==== Sorting Examples ==== 
 + 
 +Search ''index'', matching all, and sorting by ''field'' 
 + 
 +  curl -XGET 'localhost:9200/index/_search?q=*&sort=field:asc&pretty’ 
 + 
 +Search ''index'', matching all, and sorting by ''field'' 
 + 
 +  curl -XGET 'localhost:9200/index/_search?pretty' -H 'Content-Type: application/json' -d’ 
 +  { 
 +    "query": { "match_all": {} }, 
 +    "sort":
 +      { "field": "asc"
 +    ] 
 +  }' 
 +   
 +==== Limiting Results ==== 
 + 
 +Only give one result back from searching ''index'':  
 + 
 +  curl -XGET 'localhost:9200/index/_search?q=*&size=1&pretty' 
 +   
 +Give back one result, starting at search result 10, from ''index'' 
 + 
 +  curl -XGET 'localhost:9200/bank/_search?q=*&size=1&from=10&pretty' 
 + 
 +==== Aggregations ==== 
 + 
 +Finding distinct entities 
 + 
 +  curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' 
 +  { 
 +    "size": 0, 
 +    "aggs":
 +      "group_by_state":
 +        "terms":
 +          "field": "state.keyword" 
 +        } 
 +      } 
 +    } 
 +  }' 
 + 
 +===== Adding Information ===== 
 + 
 +==== Adding an index ==== 
 + 
 +  curl -XPUT 'localhost:9200/newindex?pretty’ 
 + 
 +==== Inserting documents to an index ==== 
 + 
 +Insert the JSON document (after the -d) into the index with the id ''newid'': 
 + 
 +  curl –XPUT ‘localhost:9200/index/external/newid?pretty’ –d’{“field”:”data”}’ 
 + 
 + 
 +==== Adding from a File ==== 
 + 
 +  mongoimport --db dbname --collection whichcollection --file mydata.json 
 + 
 +  curl -XPOST 'http://localhost:9200/dbname' -d @mydata.json 
 + 
 +  curl -XPOST 'http://localhost:9200/dbname/_bulk' —data-binary @mydata.json 
 +===== Deleting Information ===== 
 + 
 +  curl -XDELETE 'localhost:9200/students?pretty'
  
courses/cs397/winter2017/elasticsearch.1490634729.txt.gz · Last modified: 2017/03/27 17:12 by admin
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0