Created
October 5, 2020 21:20
-
-
Save amine-y/cb4ab353fe8d253220619eb51b7bb5dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function generateElasticSearchQueryFromTerm( $term, $lang = null ) { | |
| $term = strtolower( $term ); | |
| $query = [ | |
| 'sort'=> [ | |
| 'meta.societe_pgs_vip.raw'=> [ | |
| 'order'=> 'desc' | |
| ], | |
| // '_score' => [ | |
| // 'order'=> 'desc' | |
| // ] | |
| ], | |
| 'query' => | |
| [ | |
| 'bool' => [ | |
| 'must' => [], | |
| 'filter' => | |
| [ | |
| 'term' => [ | |
| 'post_type.raw' => 'societe' | |
| ] | |
| ] | |
| ] | |
| ] | |
| ]; | |
| $query['query']['bool']['must'][] = [ | |
| 'dis_max' => | |
| [ | |
| 'queries' => | |
| [ | |
| [ | |
| 'multi_match' => | |
| [ | |
| 'query' => $term, | |
| 'type' => 'cross_fields', | |
| 'fields' => | |
| [ | |
| 'terms.tag_societe.name', | |
| ], | |
| 'boost' => 5, | |
| ], | |
| ], | |
| [ | |
| 'wildcard' => | |
| [ | |
| 'post_title' => '*' . $term . '*' | |
| ] | |
| ], | |
| [ | |
| 'wildcard' => | |
| [ | |
| 'terms.tag_societe.name' => '*' . $term . '*' | |
| ] | |
| ], | |
| [ | |
| 'multi_match' => | |
| [ | |
| 'query' => $term, | |
| 'type' => 'cross_fields', | |
| 'fields' => | |
| [ | |
| 0 => 'post_title', | |
| ], | |
| 'boost' => 1, | |
| ], | |
| ], | |
| [ | |
| 'multi_match' => | |
| [ | |
| 'query' => $term, | |
| 'type' => 'cross_fields', | |
| 'fields' => | |
| [ | |
| 0 => '*_pds_ad_num_tel.value', | |
| ], | |
| 'boost' => 0, | |
| ], | |
| ], | |
| ], | |
| 'tie_breaker' => '0.5' | |
| ] | |
| ]; | |
| $field = [ | |
| 'postalcode' => 'meta.pgs_address_0_pds_ad_code_postal.value', | |
| 'gouv' => 'meta.pgs_address_0_pgs_ad_gouvernorat.value' | |
| ]; | |
| if(!empty($_GET['ac']) && $_GET['ac'] == 'a' && !empty($_GET['ac_id'])){ | |
| $query['query']['bool']['must'] = []; | |
| $termIds = []; | |
| $termIds[] = pll_get_term($_GET['ac_id'], 'fr'); | |
| $termIds[] = pll_get_term($_GET['ac_id'], 'en'); | |
| $termIds[] = pll_get_term($_GET['ac_id'], 'ar'); | |
| $query['query']['bool']['must'][] = [ | |
| 'terms' => [ | |
| 'terms.tag_societe.term_id' => $termIds | |
| ] | |
| ]; | |
| } | |
| if(!empty($_GET['c_type']) && !empty($_GET['postalcode'])){ | |
| $postalCodes = []; | |
| $postalCode = $_GET['postalcode']; | |
| $englishPostId = pll_get_post($postalCode, 'en'); | |
| $arabicPostId = pll_get_post($postalCode, 'ar'); | |
| if(null !== $englishPostId){ | |
| $postalCodes[] = $englishPostId; | |
| } | |
| if(null !== $arabicPostId){ | |
| $postalCodes[] = $arabicPostId; | |
| } | |
| $postalCodes[] = (int)$postalCode; | |
| $query['query']['bool']['must'][] = [ | |
| 'terms' => [ | |
| $field[$_GET['c_type']] => $postalCodes | |
| ] | |
| ]; | |
| } | |
| $langsToSearchIn = [ | |
| 'fr' => [ | |
| 'fr', | |
| 'ar', | |
| ], | |
| 'en' => [ | |
| 'fr', | |
| 'en', | |
| ], | |
| 'ar' => [ | |
| 'ar', | |
| 'fr', | |
| ] | |
| ]; | |
| $currentLang = pll_current_language(); | |
| if(null !== $lang){ | |
| $currentLang = $lang; | |
| } | |
| $query['query']['bool']['must'][] = [ | |
| 'terms' => [ | |
| 'terms.language.slug' => $langsToSearchIn[$currentLang] | |
| ] | |
| ]; | |
| //exit(json_encode($query)); | |
| return $query; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment