Elasticsearch问题

时间:2016-07-29 11:30:55

标签: elasticsearch

你能帮我解决弹性搜索问题

"script" : "  var score = 0;  
 for(var i = 0; i < _source[fieldNameTag].length; i++)
 { 
if( fieldValuesTag.indexOf(_source[fieldNameTag][i]) > -1 ) { 
score += 1;
 } 
} 
var movieName1 = _source[fieldNameMovie]; 
var movieName =movieName1.replace(/[^a-zA-Z]/g,''); 
for (var i = 0, len = movieName.length, div = 1; i < len; i++) { 
 var ascVal = movieName.charCodeAt(i);
 score += (1 - (ascVal / 100)) / div;
 div = div * 100;
 }
 score;",

3 个答案:

答案 0 :(得分:0)

首先使用以下代码进行循环

var movieName1 = _source[fieldNameMovie];
var movieName = movieName1.replace( /[^a-zA-Z]/gi , '');
movieName = movieName.toUpperCase();
for( var i=0, len = movieName.length, div = 100; i < len; i++ ){
    var ascVal = movieName.charCodeAt(i);
    var iAscVal = 100 - ascVal;
    iAscVal = (iAscVal / div).toFixed(60);
    score += iAscVal.toFixed(60);
    div = div * 100;
}
score;

答案 1 :(得分:0)

if(!is_numeric($urlParam[1]))
{
$searchData=$urlParam[1];
$searchDataArray = array($searchData);

$params = [
'index' => 'pvrmod-movie',
'type' => 'movie',
'body' => [
"from" => 0,
"size" => 10000,
'query' =>  [
'function_score'  => [
'filter'  =>  [
'bool'  =>  [
'must'  =>  [
[
'match' =>  [
'tags'  =>  [
'query' =>  $searchData,
'operator'  => 'or'
]
]
]
]
]
],
'functions' => [
[
'script_score'=> [
'params'=> [
'fieldNameTag'=> 'tags1',
'fieldValuesTag'=> $searchDataArray,
'fieldNameMovie'=> 'value'
],
'script'=> ' var score = 0;
for (var i = 0; i < _source[fieldNameTag].length; i++) {
if (fieldValuesTag.indexOf(_source[fieldNameTag][i]) > -1) {
score += 1;
}
}
score;',
'lang'=> 'javascript'
]
]
]
] 
]
]
];

$response = $client->search($params);
if($response['hits']['total'] > 0)
{
$response['hits']['hits'][0]['_source']['dataarraycount'] = $response['hits']['total'];
}

$data = $response['hits']['hits'];
$dataNew = array();

for ($i=0; $i < count($data); $i++) 
{
if($data[$i]['_score'] >= 1)
{   
$decimalScore = 0;
$movieName = $data[$i]['_source']['value'];
$movieName = preg_replace('/[^A-Za-z0-9]/', '', strtoupper($movieName));
$nameArr = str_split($movieName);
for ($i1 = 0, $len = count($nameArr), $div = 100; $i1 < $len; $i1++) 
{
$ascVal = ord($nameArr[$i1]);
$iAscVal = 100 - $ascVal;
$iAscVal = ($iAscVal / $div);
$decimalScore += $iAscVal;
$div = $div * 100;
}
$data[$i]['_score'] = intval($data[$i]['_score']) + $decimalScore;
$dataNew[] = $data[$i];
}
}

usort($dataNew, function($a,$b)
{
if ($a['_score'] == $b['_score']) 
return 0;
return ($a['_score'] > $b['_score']) ? -1 : 1;
});
for($i=0; $i < count($dataNew); $i++)
{
$dataNew[$i]['_source']['dataarraycount'] = count($dataNew);
}
$data = array_slice($dataNew, $fromRecord, $pageSize);
}

答案 2 :(得分:0)

使用以下代码

// New Code to sorting start
    $scoreArray = array();

    for ($i=0; $i < count($data); $i++) 
    {
        $currentScore = intval($data[$i]['_score']);

        if($currentScore >= 1)
        {
            $scoreArray[$currentScore][] = $data[$i];
        }
    }

    foreach ($scoreArray as $key => $groupArray) {
        array_push($dataNew, array_sort($groupArray));
    }
// New Code to sorting ends

并添加此功能

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}