Skip to content

Instantly share code, notes, and snippets.

@skynyrd
Last active December 8, 2017 16:06
Show Gist options
  • Save skynyrd/5ffdb6932c7000bdc06c0ff637bc7001 to your computer and use it in GitHub Desktop.
Save skynyrd/5ffdb6932c7000bdc06c0ff637bc7001 to your computer and use it in GitHub Desktop.
Searching and Analyzing Data - Pluralsight

Get health

curl -XGET 'localhost:9200/_cat/health?v'

Create index

curl -XPUT 'localhost:9200/products?&pretty'

Status yellow --> Don't have replica

GET All indices:

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Create a document

curl -XPUT 'localhost:9200/products/mobiles/1?pretty' -H 'Content-Type: application/json' -d'
{
    "name" : "iPhone 7",
    "camera" : "12MP",
    "storage" : "256GB",
    "display" : "4.7 inch",
    "battery" : "1960 mAh",
    "reviews": [
        "Incredibly happy after having used it for one week",
        "Best iPhone so far",
        "Very expensive, still prefer Android"
    ]
}
'

Retrieve Doc:

curl -XGET 'localhost:9200/products/mobiles/1?pretty'

Get specified fields only

curl -XGET 'localhost:9200/products/mobiles/1?pretty&_source=name,reviews'

Update whole doc:

Same with doc creation, but must include id in the url

Partial update the doc:
curl -XPOST 'localhost:9200/products/mobiles/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
    "doc" : {
        "name" : "iPhone 7 Plus"
    }
}
'

New field can be included to partial update body

Update With script

curl -XPOST 'localhost:9200/products/shoes/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
    "script": "ctx._source.size -= 1"
}
'

Delete doc:

curl -XDELETE 'localhost:9200/products/shoes/1?pretty'

Query Context: How well does this document match this query?

Filter Context: Does this document match this query clause?

For Query Context

  • Included or not: Determine whether the document should be part of the result
  • Relevance score: Calculated for every search term the document maps to
  • High score, more relevant: More relevant documents, higher in the search rankings.

For Filter Context

  • Included or not: Yes / No determines whether included in the result
  • No scoring
  • Exact matches, range queries
  • Faster

Use devtools for queries below:

Search along the document:

GET customers/personal/_search?q=wyoming

Sort from query param (No score as sorted):

GET customers/personal/_search?q=wyoming&sort=age:desc

From and size from query param:

GET customers/_search?q=state:kentucky&from=10&size=2

Query from request body

Get all docs with from, sort and size:

{
  "from": 5, 
  "size": 3,
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ], 
  "query": {
    "match_all": {}
  }
}

Using _source in query:

GET customers/personal/_search
{
  "_source": ["st*", "*n*"], 
  "query": {
    "term": {
      "state": {
        "value": "wyoming"
      }
    }
  }
}
GET customers/personal/_search
{
  "_source": {
    "includes": ["st*", "*n*"],
    "excludes": ["*der*"]
  }, 
  "query": {
    "term": {
      "state": {
        "value": "wyoming"
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment