ElasticSearch汇总请查看:ElasticSearch教程——汇总篇
在使用must,should以及must not的时候,有个需求是:某个字段可以是X,也可以是Y,甚至可以是Z(此处为描述可以是fangzhu,也可以是caoben),原本请求如下:
GET /ecommerce/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yagao"
}
}
],
"should": [
{
"match": {
"desc": ["fangzhu","caoben"]
}
}
],
"must_not": [
{
"match": {
"price": 25
}
}
],
"minimum_should_match": 1
}
}
}
结果报错
{
"error": {
"root_cause": [
{
"type": "illegal_state_exception",
"reason": "Can't get text on a START_ARRAY at 14:21"
}
],
"type": "illegal_state_exception",
"reason": "Can't get text on a START_ARRAY at 14:21"
},
"status": 500
}
网上找了很久,未在国内的任何博客或者论坛上找到解决方案,幸运的是在国外的ElasticSearch社区中找到了解决方案,故以此作为记录,原链接为[Solved] Illegal State - START_ARRAY
修改后为
GET /ecommerce/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yagao"
}
}
],
"should": [
{
"match": {
"desc": "fangzhu"
}
},
{
"match": {
"desc": "caoben"
}
}
],
"must_not": [
{
"match": {
"price": 25
}
}
],
"minimum_should_match": 1
}
}
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "J3fLFWYBBoLynJN1-kOG",
"_score": 0.36464313,
"_source": {
"name": "test yagao",
"desc": "youxiao fangzhu"
}
}
]
}
}
问题解决
更多搜索相关的可以查看
ElasticSearch教程——Kibana简单操作ES