您的位置:首页 > 移动开发

es 学习 5 DSL mapping 使用 案例

2016-08-26 15:54 309 查看
<?php
别名修改
_aliases
{
  "actions": [
    {
      "remove": {
        "index": "index1",
        "alias": "index"
      }
    },
    {
      "add": {
        "index": "index2",
        "alias": "index"
      }
    }
  ]
}
{
    "from": 0,
    "size": 20,
    "query": {
        "function_score": {
            
            "functions": [
                {
                    "gauss": {
                        "created_at": {
                            "origin": "2014/04/23 07:00:00",//何时开始
                            "scale": "2h",//控制 下降速度 缓慢,越大 下降越快
"offset": "10h"//偏移量
                        }
                    },
 "weight": 10
                }
            ],
"query": {
        "match": {
          "description": "bmw"
        }
      },
"score_mode": "multiply"
        }
    }
}
{
    "query": {
        "function_score": {
            "filter": {
                "bool": {
                    "should": {
                        "multi_match": {
                            "query": "宝马",
                            "fields": [
                                "title",
                                "advertiser_name",
                                "advertiser_name_title"
                            ]
                        }
                    }
                }
            },
            "functions": [
                {
                    "filter": {
                        "match": {
                            "advertiser_name_title": "宝马"
                        }
                    },
                    "weight": "60"
                },
                {
                    "filter": {
                        "match": {
                            "title": "bmw"
                        }
                    },
                    "weight": "40"
                },
                {
                    "filter": {
                        "match": {
                            "advertiser_name": "宝马"
                        }
                    },
                    "weight": "60"
                },
                {
                    "script_score": {
                        "script": "_score*doc['date3'].value"
                    }
                },
                {
                    "gauss": {
                        "date": {
                            "origin": "20160829",
                            "scale": "10d",
                            "offset": "5d",
                            "decay": 0.5
                        }
                    }
                }
            ],
            "score_mode": "sum"
        }
    }
}
// analyzer  : ik_max_word ik_smart ik
// search_analyzer : ik_max_word ik_smart ik
// 近义词 配置  index 案例
{
"mappings":{

"addata_index":{
"_all": {
"search_analyzer": "ik_syno_smart",
"analyzer": "ik_syno"
},
"properties": {
"id": {
"type": "float"
},
"width": {
"include_in_all": true,
"type": "string"
},
"metas": {
"type": "string",
"include_in_all": true,
"search_analyzer": "ik_syno_smart",
"analyzer": "ik_syno"
}
}
}
}
}

// ik  中文 分词 配置 案例

{
"mappings": {
"addata_index": {
"_all": {
"search_analyzer": "ik_smart",
"analyzer": "ik_max_word"
},
"properties": {
"id": {
"type": "float"
},
"width": {
"include_in_all": false,
"type": "string"
},
"tracker_full": {
"type": "string",
"include_in_all": true,
"search_analyzer": "ik_smart",
"analyzer": "ik_max_word"
},
"subject_rand": {
"type": "float",
"include_in_all": false
}
}
}
}
}

//  DSL 案例 使用

// function_score  使用
//         {
//     "query": {
//         "function_score": {
//             "filter": {
//                 "bool": {
//                     "must": [
//                         {
//                             "multi_match": {
//                                 "query": "bmw",
//                                 "fields": [
//                                     "advertiser_name",
//                                     "title",
//                                     "advertiser_name_title"
//                                 ]
//                             }
//                         }
//                     ]
//                 }
//             },
//             "functions": [
//                 {
//                     "filter": {
//                         "match": {
//                             "advertiser_name": "bmw"
//                         }
//                     },
//                     "weight": 100
//                 },
//                 {
//                     "filter": {
//                         "match": {
//                             "title": "bmw"
//                         }
//                     },
//                     "weight": 99
//                 },
//                 {
//                     "script_score": {
//                         "script": "_score + doc['created_date'].value"
//                     }
//                 }
//             ],
//             "score_mode": "sum"
//         }
//     }
// }
$curl_param = [
'from'=>$from,
'size'=>$size,
'query'=>[
'function_score'=>[
'filter'=>[
'bool'=>[
'should'=>[
'multi_match'=>[
'query'=>$param,
// 'type'=>'cross_fields',
// 'operator'=>'and',
'fields'=>[
'title',
'name'
]
]
]
]
],
'functions'=>[
[
'filter'=>[
'match'=>[
'title'=>$param
]
],
"weight"=>'10'
],
[
'filter'=>[
'match'=>[
'name'=>$param
]
],
"weight"=>'10'
],
[
'script_score'=>[
'script'=>"_score*doc['created_date'].value"
// 'script'=>"_score*doc['date2'].value"
]
]
],
// 'score_mode'=>'first'
]
]
];

// multi_match 使用
$curl_param = [
'from'=>$from,
'size'=>$size,
'query'=>[
'multi_match'=>[
'query'=>$param,
'type'=>'cross_fields',
// 'type'=>'phrase',
'operator'=>'and',
'fields'=>[
'name','title'
//'name','title'//^4
]
]
],
// 'track_scores'=>true,
// 'sort'=>[
//     'created_date'=>[
//         'order'=>'desc'
//     ]
// ]
"sort"=>[
[
'_script'=>[
'script'=>"floor(doc['date2'].value)",
'type'=>'number',
'order'=>'desc'
]
],
[
'_score'=>[
'order'=>'desc'
]
]
]
];

// aggs  统计 数据

$curl_param = [
'query'=>[
'bool'=>[
'must'=>[
'term'=>[
'id'=>$id
]
]
]
],
'aggs'=>[
'first_detected'=>[
'min'=>[
'field'=>'created_date'
]
],
'last_detected'=>[
'max'=>[
'field'=>'created_date'
]
]
]
];

//多字段查询
$curl_param = [
'sort'=>[
'id'=>[
'order'=>'desc'
]
],
'query'=>[
'multi_match'=>[
'query'=>$title,
'type'=>'best_fields',
'fields'=>[
'title','domain','keywords'
],
'tie_breaker'=>0.3,
'minimum_should_match'=>'30%'
]

]
];

// range 使用
$curl_param = [
'sort'=>[
'id'=>'desc'
],
'query'=>[
'filtered'=>[
'filter'=>[
'range'=>[
'id'=>[
'gte'=>$min_id,
'lte'=>$max_id
]
]
]
]
]
];

// group_by_state  使用
$curl_param = [
'size'=>$size,
'query'=>[
'bool'=>[
'must'=>[
'term'=>[
$field=>$value
]
],
'should'=>[
'cname'=>$param
]
],
],
'aggs'=>[
'group_by_state'=>[
'terms'=>[
'field'=>$groupBy
]
]
]
];

// sort 使用
$curl_param = [
'size'=>$size,
'sort'=>[
'date'=>[
'order'=>'desc'
]
],
'query'=>[
'bool'=>[
'must'=>[
'term'=>[
$field=>$value
]
],
'should'=>[
'cname'=>$param
]
],
],
'aggs'=>[
'group_by_state'=>[
'terms'=>[
'field'=>$groupBy
]
]
]
];
// sort 使用  多字段
// 只有满足第一个 条件时 才进行下一个条件排序
$curl_param = [
'size'=>$size,
'sort'=>[
[
'date'=>[
'order'=>'desc'
]
],
[
'id'=>[
'order'=>'desc'
]
]
],
'query'=>[
'bool'=>[
'must'=>[
'term'=>[
$field=>$value
]
],
'should'=>[
'cname'=>$param
]
],
],
'aggs'=>[
'group_by_state'=>[
'terms'=>[
'field'=>$groupBy
]
]
]
];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: