ElasticSearch类似Mysql的not in 和 in 查询

ElasticSearch 的IN查询

//查询数据在goods_id为[1,2,3]的所有数据,类似Mysql IN (1,2,3)
{
 "query":{
    "bool": {
    "must": [
        {
            "match_all":{}
        }, 
        {
            "terms": {
                "goods_id": [1,2,3]
            }
        }
        ]
    }

  }
}


//匹配查询多数用在商城首页商品搜索(推荐)

{
    "from": 0,
    "query": {
        "bool": {
            "must": [{
                "multi_match": {
                    "fields": ["goods_name", "brand"],
                    "query": "北京测试商品001-3",
                    "type": "most_fields"
                }
            }]
        }
    },
    "size": 100,
    "sort": {        
      "price": {
            "order": "desc"
        },
        "views": {
            "order": "desc"
        }
    }
}

ElasticSearch 的 NOT IN 查询

//查询数据不在goods_id为[1,2,3]的所有数据,类似Mysql NOT IN (1,2,3)
{
 "query":{
    "bool": {
    "must": [
        {
            "match_all":{}
        }
        ]
    },
    "must_not"[
            {
            "terms": 
                {
                    "goods_id": [1,2,3]
                }
            }]
        }
    ]
  }
}

 ElasticSearch 分页、排序以及多字段匹配查询

//分页、排序以及多字段匹配查询,fields为查询关键词需要匹配的字段;query关键词;type查询类型 most_fields总体算分最高的
{
    "from": 0,
    "query": {
        "bool": {
            "must": [{
                "multi_match": {
                    "fields": ["goods_name", "brand"],
                    "query": "北京测试商品001-3",
                    "type": "most_fields"
                }
            }, 
            {
                "terms": {
                    "goods_id": [6]
                }
            }]
        }
    },
    "size": 100,
    "sort": {
        "mall_sort_order": {
            "order": "desc"
        },
        "sales": {
            "order": "desc"
        }
    }
}

 ElasticSearch 按照单个字段算分从高到低进行排序


{
    "from": 0,
    "query": {
        "bool": {
            "must": [{
                "multi_match": {
                    "fields": ["goods_name", "brand"],
                    "query": "神经系统",
                    "type": "best_fields"
                }
            }]
        }
    },
    "size": 100,
    "sort": {
      "_score":{
            "order": "desc"
      }
    }
}
等同于
POST dev_shop_index/_search
{
 "query":{
  "dis_max" : {
   "queries":[
    {
     "match":{
      "goods_name" : "神经系统"
     }
    },
    {
     "match" : {
      "brand" : "神经系统"
     }
    }
    }
   ]
  }
 }
}

等同于
{
  "query": {
    "multi_match": {
        "fields": ["goods_name", "brand"],
        "query": "神经系统",
        "type": "best_fields"
    }
  } 
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
MissYou-Coding
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!