Elasticsearch操作索引

创建索引

   http://ip:port/index {json数据} put

例如:
http://192.168.118.129:9200/people
{
	"settings": {
		"number_of_shards": 3,
		"number_of_replicas": 1
	},
	"mappings": {
		"man": {
			"properties": {
				"name": {
					"type": "text"
				},
				"country": {
					"type": "keyword"
				},
				"age": {
					"type": "integer"
				},
				"date": {
					"type": "date",
					"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}
		}
	}
}

插入数据

   http://ip:port/index/type/id {json数据} put 指定id
   http://ip:port/index/type/ {json数据} post 随机生成id

例如:
http://192.168.118.129:9200/people/man/1
{
	"name": "muggle",
	"country": "china",
	"age": 18,
	"date": "2000-01-22",
	"address": "guangzhou"
}
http://192.168.118.129:9200/people/man/
{
	"name": "damon",
	"country": "china",
	"age": 18,
	"date": "2000-01-22",
	"address": "foshan"
}

修改数据

   http://ip:port/index/type/id/_update {json数据} post 直接修改文档
   http://ip:port/index/type/id/_update {json数据} post 脚本修改文档

例如:
http://192.168.118.129:9200/people/man/1/_update
{
	"doc":{
		"name": "muggles"
	}
}
http://192.168.118.129:9200/people/man/1/_update
{
	"script": {
		"lang": "painless",
		"inline": "ctx._source.age +=10"
	}
}
或
{
	"script": {
		"lang": "painless",
		"inline": "ctx._source.age = params.age",
		"params": {
			"age": 100
		}
	}
}

删除数据

   http://ip:port/index/type/id delete 删除数据
   http://ip:port/index delete 删除索引

例如:
http://192.168.118.129:9200/people/man/1
http://192.168.118.129:9200/people

查询数据

   http://ip:port/index/type/id get
   http://ip:port/index/_search {json数据} post

例如:
http://192.168.118.129:9200/people/man/1
http://192.168.118.129:9200/people/_search
{
	"query": {
		"match_all": {}
	}
}
或
{
	"query": {
		"match_all": {}
	},
	"from": 1,
	"size": 1
}
或
{
	"query": {
		"match": {
			"country": "china"
		}
	}
}
或
{
	"query": {
		"match": {
			"country": "china"
		}
	},
	"sort": [
		{"age": {"order":"desc"}}	
	]
}
或
{
	"aggs": {
		"group_by_age": {
			"terms": {
				"field": "age"
			}
		}
	}
}
或
{
	"aggs": {
		"group_by_age": {
			"terms": {
				"field": "age"
			}
		},
		"group_by_date": {
			"terms": {
				"field": "date"
			}
		}
	}
}
或
{
	"aggs": {
		"grades_age": {
			"stats": {
				"field": "age"
			}
		}
	}
}

高级查询

   1)模糊查询:

{
	"query": {
		"match": {
			"name": "damon"
		}
	}
}

   2)习语匹配:

{
	"query": {
		"match_phrase": {
			"name": "damon"
		}
	}
}

   3)多匹配查询:

{
	"query": {
		"multi_match": {
			"query": "damon",
			"fields": ["name","address"]
		}
	}
}

   4)语法查询:

{
	"query": {
		"query_string": {
			"query": "damon And foshan"
		}
	}
}
或
{
	"query": {
		"query_string": {
			"query": "damon OR guangzhou",
			"fields": ["name","address"]
		}
	}
}

   5)结构化查询:(字段查询)

{
	"query": {
		"term": {
			"age": 18
		}
	}
}

   6) 范围查询:

{
	"query": {
		"range": {
			"age": {
				"gte": 18,
				"lte": 28
			}
		}
	}
}
或
{
	"query": {
		"range": {
			"date": {
				"gte": "2000-01-22",
				"lte": "now"
			}
		}
	}
}

   7)filter过滤:

{
	"query": {
		"bool": {
			"filter": {
				"term": {
					"age":18
				}
			}
		}
	}
}

   8) 固定分数查询:

{
	"query": {
		"constant_score": {
			"filter": {
				"match": {
				"date": "2000-01-22"
				}
			}
		}
	}
}
或
{
	"query": {
		"constant_score": {
			"filter": {
				"match": {
				"date": "2000-01-22"
				}
			},
			"boost": 2
		}
	}
}

   9) bool查询:

1.或的关系
{
	"query": {
		"bool": {
			"should": [
				{
					"match": {
						"name": "damon"
					}
				},
				{
					"match": {
						"address": "guangzhou"
					}
				}
			]
		}
	}
}
2.与的关系
{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"name": "damon"
					}
				},
				{
					"match": {
						"address": "guangzhou"
					}
				}
			]
		}
	}
}
3.非的关系
{
	"query": {
		"bool": {
			"must_not": [
				{
					"term": {
						"name": "damon"
					}
				}
			]
		}
	}
}

给类型新加字段(2018-06-29更新)

   http://ip:port/index/_mapping/type/ {json数据} put

例如:
http://192.168.118.129:9200/people/_mapping/man/
{
    "man": {
        "properties": {
            "cardId": {
                "type": "integer"
            }
        }
    }
}

她想坐飞机,而我是漾船的人。