Elasticsearch Queries

Name of Innovation

Elasticsearch Queries

November 22, 2017 Uncategorized 0

DELETE /govtcompany
DELETE /company*
DELETE /test*
DELETE /myindex*

### Run from your terminal 
## it tells if elasticsearch is up or not
### 
GET /

### Create Index
PUT /govtcompany

### Get Status
GET /govtcompany1?pretty

### Get List of Indices 
GET /_cat/indices?pretty&v

GET /_cat/indices/com*?v&s=index


### Delete and Index-
DELETE /govtcompany
GET /_cat/indices?pretty

### Create index named “company”
PUT /company

### Close Index
POST /company/_close
GET /_cat/indices?pretty

#### a simple search/index on the index which is closed will lead to the exception

GET /company/_search

POST /company/_open
 
######### Index not found exception
POST /myindex1/_close
POST /myindex1/_open

### Open Index
POST /company/_open
GET /_cat/indices?pretty

### Aliases
GET /_cat/aliases?v

DELETE /test1
DELETE /test2
DELETE /test3
DELETE /test4


### Please Create Index test1,test2, test3, test4
PUT /test1
PUT /test2
PUT /test3
PUT /test4
GET /_cat/indices
GET /_cat/aliases

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

GET /_cat/aliases

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test3",
        "alias": "alias1"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "indices": [
          "test1",
          "test2"
        ],
        "alias": "alias1"
      }
    }
  ]
}
######### OK use regular expression
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test*",
        "alias": "alias1"
      }
    }
  ]
}


POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

######## use regular expression
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test*",
        "alias": "alias1"
      }
    }
  ]
}

######## will come back to this later - very intereting scenario here
### Filter Alias 
PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user": {
          "type": "keyword"
        }
      }
    }
  }
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2",
        "filter": {
          "term": {
            "user": "kimchy"
          }
        }
      }
    }
  ]
}

DELETE /test1

PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user": {
          "type": "keyword"
        }
      }
    }
  }
}

POST /test1/type1
{
  "user": "Amar Sharma1",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "Amar Sharma2",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "Amar Sharma3",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "kimchy",
  "age": 45,
  "experience": 10
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2",
        "filter": {
          "term": {
            "user": "kimchy"
          }
        }
      }
    }
  ]
}

GET /alias2/_search

### Data Insertion#########
###### schemaless
POST /company/employee
{
  "name": "Amar Sharma",
  "age": 45,
  "experience": 10
}


###### record recovery using id

GET company/employee/AV_ksw2CXaNB5r6xnSki


### Routing
PUT /my_index

PUT /my_index/my_type/1?routing=user1&refresh=true&pretty
{
  "title": "This is a document"
}

GET /my_index?pretty


PUT /my_index/my_type/1?routing=user2&refresh=true&pretty
{
  "title": "This is a document"
}

GET /my_index/my_type/1?routing=user1&pretty
GET /my_index/my_type/1?&pretty

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
  "user": "Srinivas1"
}

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
  "user": "Srinivas"
}

GET /test1/type1/1

GET /test1/type1/1?routing=amar

GET /test1/type1/1?routing=user3

DELETE company

PUT /company
{
  "mappings": {
    "employee2": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
    
  }
}

DELETE company

GET /company?pretty

### Create Mappings ( multiple )
PUT /company
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    },    
    "staff": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
    
  }
}

GET /company
### Get the mappings
GET /company?pretty

########### Data Insertion 

PUT /company1
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
  }
}

GET /company1/employee/_mapping

GET /company/_search

GET /company1/_search

DELETE /company

PUT /company
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
  }
}

POST /company/employee
{
  "name": "Amar Sharma",
  "age": 45,
  "experience": 10
}

POST /company/employee
{
  "name": "Sriknaht Kandi",
  "age": 35,
  "experience": 7
}

POST /company/employee
{
  "name": "Abdul Malik",
  "age": 25,
  "experience": 3
}

######## to specific ID ####


####### auto generated id
POST /company/employee/
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}


POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}

##### let me repeat the above one and notie the version
POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}



GET /company/employee/2


POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45
}

PUT /company/employee/2
{
  "name": "Anant Sharma",
  "age": 45,
  "FatherName": "Amar Sharma"
}

PUT /company/employee/3
{
  "name": "Anant Sharma1",
  "age": 45,
  "FatherName": "Amar Sharma"
}

PUT /company/employee/4
{
  "name": "Amar5 Sharma",
  "age": 45,
  "FatherName": "Suresh Chandra"
}

PUT /company1/employee/6
{
  "name": "Amar6 Sharma6",
  "age": 45,
  "FatherName": "Suresh Chandra"
}

###### in place updates #######

POST /company/employee/2/_update
{
  "script": "ctx._source.name='Anant2'",
  "retry_on_conflict": 3
}

###########Analyzer

POST /_analyze
{
  "analyzer": "whitespace",
  "text":     "The quick brown fox!"
}

POST /_analyze
{
  "analyzer": "standard",
  "text":     "The quick brown fox."
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding" ],
  "text":      "Is this déja vu? A quick fox jump over lazy pumped"
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding","stemmer" ],
  "text":      "Is this déja vu? A quicks foxes jumped over lazy dog"
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "synonym", "asciifolding" ],
  "text":      "Is this déja vu?"
}
POST _analyze
{
  "analyzer": "simple",
  "text":     "The 5 quic4k brown fox."
}

POST /_analyze
{
  "analyzer": "simple",
  "text":     "Time2School."
}

##########SEARCH

GET /company/employee/_search?pretty

GET /company*/staff,employee/_search?pretty

GET /_search
{
  "query": {
    "match_all": {}
  }
}

GET /company/employee/_search?pretty=true
{
  "query": {
    "query_string": {
      "query": "Amar"
    }
  }
}

GET /_search?pretty
{
  "query": {
    "match": {
      "name": "Amar Sharma"
    }
  }
}

GET /company/employee/_search?pretty=true
{
  "query": {
    "query_string": {
      "query": "Amar*"
    }
  }
}

########## careful, now filtered is no more supported
GET /company/employee/_search?pretty=true
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "name": "Amar"
        }
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "range": {
      "age": {
        "from": 15,
        "to": 50
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "range": {
      "age": {
        "from": 26,
        "to": 50
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "kandi"
          }
        },
        {
          "match": {
            "name": "Anant Sharma"
          }
        }
      ],
      "must": {
        "range": {
          "age": {
            "from": 45,
            "to": 55
          }
        }
      },
      "minimum_should_match": 2
    }
  }
}

GET /_search?pretty
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "Anant"
        }
      },
      "should": {
        "range": {
          "age": {
            "gte": 46
          }
        }
      }
    }
  }
}


GET /_search?pretty=true
{
  "query": {
    "match": {
      "name": {
        "query": "Anant Sharma",
        "operator": "or"
      }
    }
  }
}


GET /_search?pretty=true
{
  "query": {
    "match": {
      "name": {
        "query": "Anant Sharma",
        "operator": "and"
      }
    }
  }
}

POST /company/employee
{
  "name": "Abdul8 Malik8",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul9 Malik9",
  "username": "abdul 2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /_search
{
  "query": {
    "term" : { "username" : "abdul2013" } 
  }
}

POST /_search
{
  "query": {
    "term" : { "username" : "abdul 2013" } 
  }
}

GET /company/_search?pretty=true
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "username": "Amar"
          }
        },
        {
          "term": {
            "username": "abdul2013"
          }
        }
      ],
      "minimum_should_match": 0

    }
  }
}

GET /company/_search 
GET /company,company1/_search 
GET /company*/_search 
GET /company/employee/_search 
GET /company/staff,employee/_search 
GET /company/employee/_search 
GET /_all/employee/_search 

GET /company/_search?size=5&from=0 
GET /company/_search?size=5&from=5 
GET /company/_search?size=5&from=10 

GET /_search?q=name:Sharma
GET /_search?q=name:Amar5
GET /_search?q=name:Amar

POST /company/employee
{
  "name": "Abdul2 Malik2",
  "age": 25,
  "experience": 3,
  "date": "2013-04-01"
}
POST /company/employee
{
  "name": "Abdul3 Malik3",
  "age": 25,
  "experience": 3,
  "date": "2013-03-01"
}

POST /company/employee
{
  "name": "Abdul4 Malik4",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01"
}

GET /_search?q=+date:>2013-03-01+name:Amar

POST /company/employee
{
  "name": "Abdul4 Malik4",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

######## are same #######
GET /_search?q=_all:Amar
GET /_search?q=Amar

############################

POST /company/employee
{
  "name": "Abdul7 Malik7",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul7 Malik7",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul8 Malik8",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul9 Malik9",
  "username": "abdul 2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

GET /company/_mapping

GET /company/_search?q=2013

GET /company/_search?q=date:2013

POST /company/employee
{
  "name": "Andrew Thomas",
  "username": "Abdul9 Malik9",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "David Knol"
}

GET /_search?pretty
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "Andrew Gabbar"
        }
      },
      "should": {
        "range": {
          "age": {
            "gte": 15
          }
        }
      }
    }
  }
}




POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "myindex",
        "alias": "alias1"
      }
    }
  ]
}

PUT /myapp
{
  "mapping": {
    "tweet": {
      "properties": {
        "tweet": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "nick": {
          "type": "string"
        },
        "Date": {
          "type": "date", "format": "yyyy-MM-dd HH:mm:ss"
        },
        "rt": {
          "type": "long"
        }
      }
    }
  }
}

POST _analyze
{
  "analyzer": "whitespace",
  "text":     "The Brown-Cow's Part_No. #A.BC123-456 joe@bloggs.com"
}