【问题标题】:How to get specific data from algolia in angular如何以角度从 algolia 获取特定数据
【发布时间】:2021-03-02 16:31:35
【问题描述】:

我在 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获取具体数据?
任何帮助将不胜感激,
谢谢

【问题讨论】:

    标签: angular algolia instantsearch.js


    【解决方案1】:

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

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

    请注意,您的product_id 属性必须在attributesForFaceting 中设置才能过滤。如果您只需要将其用作过滤器而不是将其用作构面,则可以使用 filterOnly 修饰符以获得更好的性能。这需要在索引级别通过您的索引代码或直接在 Algolia 仪表板中完成。您可以关注this guide 了解如何操作。

    【讨论】:

      【解决方案2】:

      通过阅读本页 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;
                 });
            }
          }
      

      【讨论】:

      • @jMansour 感谢您的回答,但我只想要特定 product_id 的单个记录。该怎么做?
      猜你喜欢
      • 1970-01-01
      • 2022-11-07
      • 2018-11-06
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2023-04-07
      • 2021-02-28
      • 1970-01-01
      相关资源
      最近更新 更多