es 学习 5 DSL mapping 使用 案例

<?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
                    ]
                ]
            ]
        ];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值