代码之家  ›  专栏  ›  技术社区  ›  adam78

Algolia-使用php api客户端放置自动完成

  •  0
  • adam78  · 技术社区  · 7 年前

    我一直在玩Algolia自动完成的地方。js图书馆。当你使用图书馆时,你会得到一份建议清单。

    {
      "query": "pari",
      "suggestion": {
         "name": "Paris",
         "administrative": "Île-de-France",
         "country": "France",
         "countryCode": "fr",
         "type": "city",
         "latlng": {
         "lat": 48.8546,
         "lng": 2.34771
       },
      "postcode": "75000",
      "highlight": {
          "name": "<em>Pari</em>s",
          "administrative": "Île-de-France",
          "country": "France"
       },
       "value": "Paris, Île-de-France, France"
     }
    }
    

    我需要使用php客户端,并返回我自己的应用程序api的建议列表,例如:。

    $places = \AlgoliaSearch\Client::initPlaces();
    
    $result = $places->search($term, [
    
            'type' => ['city', 'country', 'address'],
            'language' => 'en',
            'aroundLatLngViaIP' => false,
    
    ]);
    
    dd($result);
    

    然而,当您使用php客户端(注意,我在本例中使用的是laravel scout)时,您不会得到一个建议列表,也就是说,没有任何建议 value 属性(找到的位置的完整显示名称),您可以将其返回给最终用户,而不是返回以下响应?

     {
       "hits": [{
       "objectID": "145746683_7444",
          "locale_names": {
             "default": ["Paris"],
        },
       "city": {
           "default": ["Paris"],
        },
       "county": {
           "default": ["Paris"],
       },
       "administrative": ["Île-de-France"],
       "country": {
           "default": "France",
       },
       "country_code": "fr",
       "postcode": ["75000"],
       "population": 2243833,
       "_geoloc": {
           "lat": 48.8564,
           "lng": 2.3521
       },
       "_highlightResult": {
           "locale_names": {
              "default": [{
                  "value": "<em>Paris</em>",
                  "fullyHighlighted": true,
                  "matchedWords": ["paris"],
                  "matchLevel": "full"
              }]
         },
    
       }
     }],
     "nbHits": 1,
     "query": "Paris"
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Olivier Lance    6 年前

    如果你看看 Algolia Places JavaScript library ,这是他们在将数据交给自动完成之前正在处理的数据:

        const name = hit.locale_names[0];
        const country = hit.country;
        const administrative =
          hit.administrative && hit.administrative[0] !== name
            ? hit.administrative[0]
            : undefined;
        const city = hit.city && hit.city[0] !== name ? hit.city[0] : undefined;
        const suburb =
          hit.suburb && hit.suburb[0] !== name ? hit.suburb[0] : undefined;
    
        const county =
          hit.county && hit.county[0] !== name ? hit.county[0] : undefined;
    
        const { postcode, highlightedPostcode } =
          hit.postcode && hit.postcode.length
            ? getBestPostcode(hit.postcode, hit._highlightResult.postcode)
            : { postcode: undefined, highlightedPostcode: undefined };
    
        const highlight = {
          name: getBestHighlightedForm(hit._highlightResult.locale_names),
          city: city
            ? getBestHighlightedForm(hit._highlightResult.city)
            : undefined,
          administrative: administrative
            ? getBestHighlightedForm(hit._highlightResult.administrative)
            : undefined,
          country: country ? hit._highlightResult.country.value : undefined,
          suburb: suburb
            ? getBestHighlightedForm(hit._highlightResult.suburb)
            : undefined,
          county: county
            ? getBestHighlightedForm(hit._highlightResult.county)
            : undefined,
          postcode: highlightedPostcode,
        };
    
        const suggestion = {
          name,
          administrative,
          county,
          city,
          suburb,
          country,
          countryCode: findCountryCode(hit._tags),
          type: findType(hit._tags),
          latlng: {
            lat: hit._geoloc.lat,
            lng: hit._geoloc.lng,
          },
          postcode,
          postcodes: hit.postcode && hit.postcode.length ? hit.postcode : undefined,
        };
    
        // this is the value to put inside the <input value=
        const value = formatInputValue(suggestion);
    

    您应该可以在这段代码中找到所需的全部内容,更具体地说,对于找到的位置的完整显示名称,它是基于以下内容构建的(其中 suggestion 从上面的代码中可以看到下面代码的呈现上下文):

     const out = `${name}${type !== 'country' && country !== undefined ? ',' : ''}
     ${city ? `${city},` : ''}
     ${administrative ? `${administrative},` : ''}
     ${country ? country : ''}`
        .replace(/\s*\n\s*/g, ' ')
        .trim();
      return out;
    
    

    (在 formatInputValue.js file `)