- 全局搜索
GET /shops_index,goods_index/_search //多索引搜索用逗號隔開或者 /_all/_search、/_search
{
"query": {
"multi_match": {
"query": "口腔", // 如果查詢字段 *口腔* 則fileds 必須["goods_title.keyword^10","goods_subtitle.keyword^7"] ^1表示權重
"fields": ["goods_title", "title"]
}
}
"_source": ["goods_title","title"] //查詢字段
}
- match_all
{
"query":{
"match_all":{} //查詢所有
}
}
- 分頁查詢
{
"query": {
"match_all":{} //查詢所有
},
"from":0,
"size":10
}
- match_phrase
查詢同一個字段中幾個詞,可以跳過其他詞,slop表示可以跳過的最大詞數
{
"query": {
"match_phrase": {
"description": "He is",
"slop":100
}
}
}
- term 單個條件查詢
(不會分詞,精確匹配)一句話解釋: elasticsearch 里默認的IK分詞器是會將每一個中文都進行了分詞的切割,所以你直接想查一整個詞,或者一整句話是無返回結果的。查詢不是中文的字符串能查不出來
{
"query": {
"term": {
"category_id": "10052", // 能查出數據
//"goods_title" : "體檢", 查不出來因為中文分詞了,除非就一個漢字
}
}
}
- terms 多個關鍵字查詢
"query": {
"terms": {
"channel_id": [102,101]
}
}
- match 條件查詢
會分詞,全文檢索
{
"query": {
"match": {
"goods_title": "體檢"
}
}
}
{
"query": {
"match": {
"goods_title": {
"query": "體檢",
"operator": "and" // or 表示只要有一個分詞就可以查詢出來,and 表示必須包含分出來的所有字段
}
}
}
}
- multi_match 多個字段檢索
{
"query": {
"multi_match": {
"query": "口腔",
"fields": ["goods_title","goods_subtitle"]
}
}
}
- exits 檢索字段存在
{
"query": {
"exists": {
"field": "goods_title"
}
}
}
- bool 檢索
must :多個條件全部要滿足;should:或者的意思,滿足一個即可;must_not:除了滿足所有條件剩下的數據
{
"query": {
"bool": {
"must": [
{
"multi_match":{
"query":"口腔",
"fields":["goods_title","goods_subtitle"]
}
},
{
"term":{
"age":19
}
},
{
"terms": {
"brand_name.keyword": ["白敬宇"] //類似in查詢
}
}
],
"must_not":[
{
"terms": {
"goods_id": [1,2,3]
}
}
]
}
}
}
- post_filter 對結果過濾
{
"query": {
"match": {
"goods_title": "口腔"
}
},
"post_filter": {
"range": {
"selling_price": {
"gte": 1000,
"lte": 1500
}
}
}
}
- sort 排序
{
"query": {
"match": {
"goods_title": "口腔"
}
},
"post_filter": {
"range": {
"selling_price": {
"gte": 1000,
"lte": 2000
}
}
},
"sort": [
{
"selling_price": {
"order": "asc"
}
}
],
"_source": ["selling_price"]
}
- 修改setting
注意:修改setting需要先關閉所以,然后再開啟索引
$es->indices()->close(['index' => $index]);
$params_setting = [
'index' => $index,
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [
'ik_pinyin_analyzer' => [
'type' => 'custom',
'tokenizer' => 'ik_max_word',
'filter' => ["my_pinyin", "word_delimiter"],
]
],
'filter' => [
"my_pinyin" => [
"type" => "pinyin",
"first letter" => "prefix",
"padding_char" => " "
]
]
]
]
]
];
$es->indices()->putSettings($params_setting);
$es->indices()->open(['index' => $index]);
- 修改mApping
PUT fit_goods_index/_mapping
{
"properties":{
"search_title":{
"type":"text"
}
}
}
// php代碼
$params = [
'index' => $index,
'body' => [
'_source' => [
'enabled' => true
],
'properties' => [
'location' => [
'type' => 'geo_point',
'ignore_malformed' => true
],
'completion' => [
'type' => 'completion',
'analyzer' => 'ik_pinyin_analyzer',
'fields' => [
"key" => [
"type" => "keyword"
],
],
"contexts" => [
[
"type" => "category",
"name" => "plan_id"
]
]
],
]
]
];
$es->indices()->putMapping($params);
- 范圍搜索
GET /goods_index/_search
{
"query":{
"bool":
{
"must":[
{
"query_string":{
"query":"雙重好禮",
"fields": ["goods_title^2"]
}
}
]
}
},
"_source": ["goods_title","goods_subtitle","selling_price"],
"post_filter": {
"range": {
"selling_price": {
"gte": 0,
"lte": 108
}
}
}
}
- in 查詢
GET /goods_index/_search
{
"query":{
"bool":
{
"must":[
{
"query_string":{
"query":"雙重好禮",
"fields": ["goods_title"]
}
},
{
"terms": {
"specs_type": ["more"]
}
}
]
}
}
}
NOT IN 查詢
GET /goods_index/_search
{
"query":{
"bool":
{
"must":[
{
"query_string":{
"query":"雙重好禮",
"fields": ["goods_title"]
}
}
],
"must_not":[
{
"terms":
{
"specs_type": ["more"]
}
}
]
}
}
}