angular 如何从 algolia 获取特定数据

How to get specific data from algolia in angular

我在 angular.
中使用 algolia 我试图从 algolia.
中获取特定记录 我有一个 product_id 并且想要那个 product_id
的记录 这是我到目前为止所做的 -
.html 文件

    <ais-instantsearch [conproducts]>
        <ais-configure [searchParameters]="{ hitsPerPage: 1 }"></ais-configure>
        <ais-hits>
           <ng-template let-hits="hits">
           <div class="row">
                <div class="col-lg-2 col-md-3 col-sm-4 col-6"*ngFor="let hit of hits">
                    {{hit.name}}
                 </div>
           </div>
           </ng-template>
         </ais-hits>
    </ais-instantsearch>

.ts 文件

    const searchClient = algoliasearch(
      environment.algolia_application_id,
      environment.algolia_search_api_key
    );
    
    export class ProductsComponent implements OnInit {
      productsConfig = {
          indexName: 'products',
          searchClient
      };
    }

如何在algolia中通过id获取特定数据?
任何帮助将不胜感激,
谢谢

通过阅读 algolia 文档的这一页:

https://www.algolia.com/doc/api-reference/api-methods/search/

.ts 文件

const searchClient = algoliasearch(
      environment.algolia_application_id,
      environment.algolia_search_api_key
    );
    
    export class ProductsComponent implements OnInit {

      hits: any;

      productsConfig = {
          indexName: 'products',
          searchClient
      };

      OnInit(){
           const index = searchClient.initIndex(productsConfig.indexName);

           index.search('query string', {
                    attributesToRetrieve: ['name','firstname', 'lastname'],
                    hitsPerPage: 50,
            }).then(({ hits }) => {
                console.log(hits); // check the console 
                this.hits = hits;
           });
      }
    }

如果您尝试仅通过 Algolia 索引中的属性检索特定记录,则可以使用 filters。例如,假设您要检索 product_id 为“abcdef”的记录,您可以在 ais-configure 组件上设置过滤器,如下所示:

<ais-configure
  [searchParameters]="{ filters: 'product_id:abcdef' }"
></ais-configure>

请注意,您的 product_id 属性必须在 attributesForFaceting for it to be filterable. You can use the filterOnly modifier if you only need to use it as a filter and never as a facet, for better performance. This needs to be done at the index level, either from your indexing code or directly in the Algolia dashboard. You can follow this guide 中设置才能了解如何操作。