head插件安装(google)本地访问地址:http://127.0.0.1:9200/_plugin/head/
RESTful接口URL的格式:
http://localhost:9200/<index>/<type>/[<id>]
1、使用对应分词器进行分析:
url:http://127.0.0.1:9200/i_entity/_analyze?analyzer=pinyin&pretty=true
body:{"text":"中国人民"}
method:POST
2、 索引文档的创建:
url:http://127.0.0.1:9200/song001/list001/1
body:{"number":32768,"singer":"杨坤","size":"5109132","song":"今夜二十岁","tag":"中国好声音","timelen":319}
method:PUT
3、索引文档的查询:
url:http://127.0.0.1:9200/song001/list001/1
method:GET
4、索引文档的更新(和创建是一样的,索引版本递增;created字段是false,表示这次不是新建而是更新)
url:http://127.0.0.1:9200/song001/list001/1
body:{"number":32768,"singer":"杨坤","size":"5109132","song":"今夜二十岁","tag":"中国好声音","timelen":319}
method:PUT
5、索引文档的删除
url:http://127.0.0.1:9200/song001/list001/1
method:DELETE
6、索引删除
url:http://127.0.0.1:9200/index-*
method:DELETE
----------------------------------------------------
7、使用分词器分析
url:http://127.0.0.1:9200/new_zxsy/_analyze?analyzer=pinyinSimpleIndexAnalyzer&pretty=true
method:POST
body:{"text":"未来简史"}
ES默认分词器:ik_max_word,ik_smart,pinyin,pinyin_first_letter,NGram,Edge_NGram
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 50
},
8、聚合查询
-------------------------------------------------
(1)求field的平均值
http://127.0.0.1:9200/i_entity/t_entity/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
注:avg_score为自定义
9、ES Java API查询
---------------------------------------------------
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(里面各种QueryBuilders.xxxQuery)
.withIndices("i_entity")
.withTypes("t_entity")
.withSort(SortBuilders.scoreSort())
.withPageable(pageable) //Pageable pageable = new PageRequest(Long.valueOf(0).intValue(), Long.valueOf(10).intValue());
.build()
QueryBuilders.boolQuery() //连接多个查询
QueryBuilders.termQuery("name", "人类简") //精确匹配
QueryBuilders.matchQuery("name", "人类简") //分词匹配
QueryBuilders.matchQuery("name", "人类简").operator(Operator.AND) //可以使用Operator.AND使所有分词以AND的形式匹配,默认是OR
QueryBuilders.prefixQuery("name", "中国");
9、term精确匹配
{
"query": {
"bool": {
"should": [
{
"term": {
"pinyin": "w"
}
},
{
"term": {
"jianpin": "wei"
}
}
]
},
"from": 0,
"size": 100
}
}
10、match分词匹配
{
"query": {
"multi_match": {
"query": "未来",
"fields": [
"name","pinyin","jianpin"
]
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
_search
{
"query": {
"multi_match": {
"query": "未来",
"fields": [
"zhongwen^3",
"pinyin",
"jianpin"
],
"type":"best_fields"
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
{
"query": {
"multi_match": {
"query": "wlj",
"fields": [
"zhongwen^10",
"pinyin^3",
"jianpin"
]
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
12 ------------Bool搜索-------------------
http://127.0.0.1:9200/zxsy/goodsearch/_search
post
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "li xiao lai",
"fields": [
"goodsName^5",
"goodsFullPinyin^3",
"goodsPinyin^3",
"goodsJianpin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "li xiao lai",
"fields": [
"author",
"authorFullPinyin",
"authorPinyin",
"authorJianpin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "li xiao lai",
"fields": [
"speakerName",
"speakerFullPinyin",
"speakerPinyin",
"speakerJianpin"
],
"type": "best_fields"
}
}
]
}
},
"from": 0,
"size": 10
}
------------------------------------------------------------
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "未来",
"fields": [
"goodsName^5",
"author^3",
"speakerName"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "未来",
"fields": [
"goodsPinyin^5",
"authorPinyin^3",
"speakerPinyin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "未来",
"fields": [
"goodsJianpin^5",
"authorJianpin^3",
"speakerJianpin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "未来",
"fields": [
"goodsFullPinyin^5",
"authorFullPinyin^3",
"speakerFullPinyin"
],
"type": "best_fields"
}
}
]
}
},
"from": 0,
"size": 10
}
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "未来简史",
"fields": [
"goodsName^5",
"author^3",
"speakerName"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "weilaijianshi",
"fields": [
"goodsPinyin^5",
"authorPinyin^3",
"speakerPinyin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "未来简史",
"fields": [
"goodsJianpin^5",
"authorJianpin^3",
"speakerJianpin"
],
"type": "best_fields"
}
},
{
"multi_match": {
"query": "weilaijianshi",
"fields": [
"goodsFullPinyin^5",
"authorFullPinyin^3",
"speakerFullPinyin"
],
"type": "best_fields"
}
}
]
}
},
"from": 0,
"size": 10
}
"tie_breaker": "0.3"
-----------------------------------------------
13、为索引添加别名
-----------------------------------------------
url: http://127.0.0.1:9200/new_zxsy/_aliases
method: PUT
body:
{
"actions": [
{
"add": {
"index": "new_zxsy",
"alias": "alias_zxsy_search"
}
}
]
}
14、删除索引别名
-------------------------------------
url:http://127.0.0.1:9200/zxsy/_aliases/
method:PUT
body:
{
"actions": [
{
"remove": {
"index": "zxsy",
"alias": "alias_zxsy"
}
}
]
}
14、使用自定义得分搜索
---------------------------------------
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "简史",
"fields": [
"goodsName^10",
"goodsFullPinyin",
"author^2",
"speakerName"
]
}
},
"field_value_factor": {
"field": "goodsId",
"modifier": "none",
"factor": 0.1
},
"boost_mode": "sum"
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "简史",
"fields": [
"goodsName^10",
"goodsFullPinyin",
"author^2",
"speakerName"
]
}
},
"field_value_factor": {
"field": "attentWeight",
"modifier": "log1p",
"factor": 1.0
},
"boost_mode": "sum"
}
},
"from": 0,
"size": 50,
"sort": [],
"aggs": {}
}
---------------------------------
15、搜索增加多级排序
{
"query": {
"multi_match": {
"query": "简史",
"fields": [
"goodsName^10",
"goodsFullPinyin",
"author^2",
"speakerName"
]
}
},
"from": 0,
"size": 15,
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"goodsId": {
"order": "desc"
}
}
],
"aggs": {}
}
16、------------聚合统计-自定义:return_goodsType_count-------------
{
"query": {
"multi_match": {
"query": "简史",
"fields": [
"goodsName^10",
"goodsFullPinyin",
"author^2",
"speakerName"
]
}
},
"size": 0,
"aggs": {
"return_goodsType_count": {
"terms": {
"field": "goodsType"
}
}
}
}
17--------------前缀搜索与模糊搜索---------------
{
"query": {
"bool": {
"should": [
{
"query": {
"prefix": {
"goodsNameNotAnalyzed": {
"value": "大咖"
}
}
}
},
{
"query": {
"wildcard": {
"goodsNameNotAnalyzed": "*大咖*"
}
}
},
{
"query": {
"prefix": {
"goodsFullPinyin": {
"value": "dakalin"
}
}
}
},
{
"query": {
"wildcard": {
"goodsFullPinyin": "*dakalin*"
}
}
}
]
}
},
"from": 0,
"size": 15,
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"clickCount": {
"order": "desc"
}
}
],
"aggs": {}
}
-------------------------------------