Bonjour,
J'ai créé un moteur de recherches avec ElasticSearch. Ci joint le code produit.
Dans mon index Elastic Search, j'ai 2 pois : C La Coiffure Astrid Coiffure
J'ai lancé une recherche "Astrid coiffure"
Je voudrais que Astrid coiffure sorte avant C La Coiffure, tu peux m'aider ? Si les 2 mots recherchés sont dans un des résultats, son score devrait avoir un poids plus important.
Merci de votre aide.
1public function searchPois($search, $size = 1000, $from = 0): array 2{ 3 $queryParameters['index'] = ['pois']; 4 $queryParameters['body']['size'] = $size; 5 $queryParameters['body']['from'] = $from; 6 $queryParameters['body']['sort']['_score'] = ['order' => 'desc']; 7 8 if (!empty($this->sortEnterpriseElasticScript())) { 9 $queryParameters['body']['query']['function_score'] = $this->sortEnterpriseElasticScript(); 10 } 11 12 if ($search->what->exactSearch) { 13 $queryParameters['body']['query']['function_score']['query']['bool']['must'][0]['match_phrase']['name']['query'] = implode(' ', $search->what->tags); 14 } else { 15 if ($search->what->tags) { 16 $search->what->tags = $this->removeUnsedWords($search->what->tags); 17 18 if (count($search->what->tags) === 1) { 19 $tag = Tag::query() 20 ->where([ 21 'type' => 'poi', 22 'slug' => str($search->what->tags[0])->slug()->value() 23 ]) 24 ->first(); 25 26 if ($tag) { 27 $term = [ 28 'term' => [ 29 'tags.id' => $tag->id, 30 ], 31 ]; 32 33 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 34 35 $search->what->tags[0] = $tag->name; 36 } else { 37 $queryParameters['body']['query']['function_score']['query']['bool']['must'][0]['match']['name']['query'] = implode(' ', $search->what->tags); 38 } 39 } else { 40 $queryParameters['body']['query']['function_score']['query']['bool']['must'][0]['match']['name']['query'] = implode(' ', $search->what->tags); 41 } 42 } 43 44 if ($search->what->category) { 45 $term = [ 46 'term' => [ 47 'categories.id' => $search->what->category->id, 48 ], 49 ]; 50 51 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 52 } elseif ($search->what->subcategory) { 53 $term = [ 54 'term' => [ 55 'subcategories.id' => $search->what->subcategory->id, 56 ], 57 ]; 58 59 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 60 } elseif ($search->what->firm) { 61 $term = [ 62 'term' => [ 63 'firm_id' => $search->what->firm->id, 64 ], 65 ]; 66 67 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 68 } 69 70 if ($search->where->address) { 71 $term = [ 72 'term' => [ 73 'address_keyword' => $search->where->address, 74 ], 75 ]; 76 77 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 78 } 79 } 80 81 if ($search->where->metro) { 82 $term = [ 83 'term' => [ 84 'metros.id' => $search->where->metro->id, 85 ], 86 ]; 87 88 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term; 89 } elseif ($search->where->listOfAroundCities->isNotEmpty()) { 90 $terms = [ 91 'terms' => [ 92 'city.id' => $search->where->listOfAroundCities->pluck('id')->toArray(), 93 ], 94 ]; 95 96 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $terms; 97 } elseif ($search->where->city) { 98 $term = [ 99 'term' => [100 'city.id' => $search->where->city->id,101 ],102 ];103104 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term;105 } elseif ($search->where->department) {106 $term = [107 'term' => [108 'department.id' => $search->where->department->id,109 ],110 ];111112 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = $term;113 }114115 if ($search->withPromotion) {116 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = [117 'exists' => [118 'field' => 'promotion'119 ]120 ];121 }122123 if ($search->atHome) {124 $queryParameters['body']['query']['function_score']['query']['bool']['must'][] = [125 'term' => [126 'at_home' => true127 ]128 ];129 }130131 return $this132 ->client133 ->search($queryParameters)134 ->asArray();135}136137private function sortEnterpriseElasticScript(): array138{139 $painlessScript = "140 double score = 0;141 double note = doc.containsKey('average') && !doc['average'].empty ? doc['average'].value : 0;142 double numberOfNotes = doc.containsKey('number_of_notes') && !doc['number_of_notes'].empty ? doc['number_of_notes'].value : 0;143 double reduceScore = 1;144 double coefficientIfIsSelected = doc.containsKey('selected') && doc['selected'].value ? 50 : 0;145 if (note <= 3.5) { reduceScore = 10; }146 double increaseScore = (numberOfNotes >= 5 && note >= 4) ? 5 : 1;147 score = ((numberOfNotes / params.numberOfReviews) / reduceScore + (note * increaseScore / params.numberOfNotes) * params.multiplyingFactor) + coefficientIfIsSelected;148 return score;149 ";150151 return [152 'script_score' => [153 'script' => [154 'lang' => 'painless',155 'source' => $painlessScript,156 'params' => [157 'numberOfReviews' => 1, // ??158 'numberOfNotes' => 1, // ??159 'multiplyingFactor' => 2,160 ],161 ],162 ],163 ];164}
Aucune réponse