0
点赞
收藏
分享

微信扫一扫

Elasticsearch整理笔记(四)


filter组合查询:

POST ​​http://localhost:9200/order_server/order/_search​​

{
"_source":false,
"query": {
"bool": {
"filter": [
{"term":{
"status":1
}},
{"term":{
"extras.student_name":"张三"
}},
{"range":{
"status":{"gte":1,"lt":100}
}},
{"terms":{
"status":[1,2,3,4]
}},
{"wildcard":{
"context":"*长河*"
}},
{"multi_match":{
"query": "佳木斯第二中学",
"fields": ["extras.school_name", "extras.grade"],
"type":"most_fields"
}}
]
}
}
}

其中,"_source":false为不需要返回数据,_id可以从"hits"."hits"[]."_id"获取。

如果是默认的分词,term精确查询会查不到,因为es已经进行了拆分。如果不需要拆分业务,创建索引时可以设置默认不分词。

PUT ​​http://localhost:9200/order_server​​

{
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "5"
}
},
"analysis": {
"analyzer": {
"default": {
"type": "keyword"
}
}
}
}

不分词的模糊搜索可以使用wildcard模糊搜索,在value的前后都加上通配符*即可。

{
"_source": false,
"query": {
"bool": {
"filter": [{
"wildcard": {
"order_id":"*3*"
}
}]
}
}
}

注意,这个order_id必须为string类型,如果是整型则会报错

{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "……",
"index_uuid": "……",
"index": "……"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "……",
"node": "……",
"reason": {
"type": "query_shard_exception",
"reason": "……",
"index_uuid": "……",
"index": "……",
"caused_by": {
"type": "number_format_exception",
"reason": "For input string: \"*3*\""
}
}
}
]
},
"status": 400
}

 

text类型的字段是默认不支持排序的,如果需要排序,可以针对字段额外设置fielddata,配置后,历史数据可以支持排序。

PUT ​​http://localhost:9200/order_server/_mapping/order​​

{
"properties":{
"user_id":{
"type": "text",
"fielddata": true
}
}
}

 

举报

相关推荐

0 条评论