【问题标题】:Angular4 with Azure Search search string not working带有 Azure 搜索搜索字符串的 Angular4 不起作用
【发布时间】:2018-05-02 09:57:09
【问题描述】:

我正在使用 Azure 搜索开发一个 Angular4 应用程序。我需要根据搜索字符串拨打电话以获取产品。 HTML 是:

<input name="searchQuery" class="form-control" id="" [(ngModel)]="query">
<button class="btn btn-default" type="submit" [routerLink]="['/search', query]" (click)="search()">Suchen</button>

搜索组件:

export class SearchResultQueryComponent implements OnInit {
  public query: string;
  private products: Product[];    

  @Output() searchEvent: EventEmitter<Product[]> = new EventEmitter();
  private searchTerms = new Subject<string>();

  constructor(private route: ActivatedRoute,) {
    this.products = [];
  }

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.query = params.query;
    });
  }

  search() {
    if (this.query != null && this.query !== '' && this.query.length !== 0) {
      this.searchTerms.next(this.query);
      this.azureSearchApiService.search(this.searchTerms, ['brand'], 0)
        .subscribe(p => {
          this.products = p.value;
        });
      this.searchEvent.emit(this.products);
    } else {
      return;
    }
  }
}

父组件:

searchResult(products: Product[]) {
    if (this.products != null) {
      this.products = products;
    }
  }

搜索服务:

headers = new Headers();

  constructor(private http: Http, private translate: TranslateService) {
    this.headers.append('Content-Type', 'application/json');
    this.headers.append('api-key', '73E96D367A256255E88DA72931E4CD72');
  }
search(terms: Observable<string>, facets: string[], page: number) {
    return terms
      .distinctUntilChanged()
      .switchMap(term => this.searchEntries(term, facets, page) );
  }

  searchEntries(term, facets: string[], page: number) {
    const requestOptions = new RequestOptions({ headers: this.headers });
    const searchOptions = this.searchOptions(term, facets, page);
    return this.http
      .post('the url....', searchOptions, requestOptions)
      .map(res => res.json())
  }

  searchOptions(searchTerm: string, facets: string[], page: number): any {
    const options = {
      search: '*' + searchTerm,
      facets: [ facets.join(',')],
      // paging
      top: 5,
      skip: 5 * page,
      count: true,
    };
    return JSON.stringify(options);
  }

问题是: 尽管搜索词是正确的,但我总是收到相同的结果。第二个问题是我必须点击按钮三次才能收到结果。我不明白为什么?怎么办?

任何想法都会有所帮助。谢谢你

【问题讨论】:

  • 可能是错误的标签?您的代码表明您正在使用 Azure 搜索。
  • 是的,我正在使用 azure 搜索,抱歉
  • 我找到了第一个问题的解决方案,我必须添加:queryType: 'full' 并删除 * 但第二个问题我直到现在都无法解决

标签: angular azure azure-cognitive-search


【解决方案1】:

我找到了解决方案,我使用“Redux”框架作为有状态列表。它对我有用。延迟是在事件发生后更新 GUI

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 2012-01-04
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2011-04-21
    相关资源
    最近更新 更多