LoginSignup
6
5

More than 3 years have passed since last update.

自社サイトにBing Custom Searchを設置(サイト内検索)

Last updated at Posted at 2020-03-04

Bing Custom Searchの設置方法

公式マニュアルが分かりにくいので備忘録。

microsoft公式マニュアル: Bing Custom Searchクイック スタート

Google Custom Searchは広告がひっついてくるのでBingにリプレイスしようと。Google Custom Searchと同じように<script>タグを埋め込めばいいのだろうとマニュアルを見たが分かりにくい。
【結論】<script>タグを埋め込めばよい。

以下手順。

AzureでCognitive Servicesのサブスクリプションキーを取得

まずはサブスクリプションキーをば。

  • Azure PortalのCognitive Services画面にいく
    image.png

  • 追加ボタン
    image.png

  • Bing Custom Searchを選択
    image.png

  • 作成ボタン
    image.png

  • 名前、サブスクリプションの分類、価格レベル(無料枠であればF0を選択)、リソースグループを入力して作成
    image.png

  • 作成できたらキーとエンドポイントができてる。このキーがサブスクリプションキー
    image.png

Bing Custom Search インスタンスの作成

これは公式マニュアル通りの手順で良い。

  • Bing Custom Search ポータル へ(https://www.customsearch.ai/applications)
  • New Instance
    image.png

  • instance名を入力
    image.png

  • 自サイトのドメイン名を入力して+をクリック
    image.png

  • インスタンスができたらHosted UIをクリックして表示オプション的な設定をしていくことになる。
    image.png

  • ①~③は見た目の設定なので適当に。ただ④でSearch Keyを設定しないと機能しない。ここにサブスクリプションキーを入力する(Search KeyじゃなくてSubscription Keyと書いてほしい汗)
    image.png

  • そうしたら画面右上のPublishボタンをクリック
    image.png

  • Productionページに行きますか?それともこのページにとどまりますか?と聞かれるのでProductionページ行くをクリック。次のような画面に
    image.png

  • このページでいちおうクエリを試す(やらなくてもいいけど)
    image.png
    CallをクリックでJSONが返ってくるはず。

  • Hosted UIをクリック
    image.png

  • この画面にある<Script>コードを自サイトに埋め込めばよい。
    image.png

Bing Custom Search エンドポイントを呼び出す

次の手順、とマニュアルにはNode.js を使用して Bing Custom Search エンドポイントを呼び出す に行くボタンがあるが、これも必要な手順かと迷うが、いらない。表示画面をカスタマイズしたいとかなければ必要ない。

スクリプトコードを埋め込む

当方Angular8なのでスクリプトコードは埋め込めない。
以下のやり方でやった。

.ts


  ngOnInit() {

    const script = document.createElement('script');
    script.async = true;
    script.type="text/javascript";
    script.src = 'https://ui.customsearch.ai/api/ux/rendering-js?customConfig=fcbxxxx3-xxxx-xxxxx-xxxxx-eexxxxa516c2&market=ja-JP&version=latest&q=';
    script.id = 'bcs_js_snippet';

    const div = document.getElementById('script');
    div.insertAdjacentElement('afterend', script);
  }

HTML

<div id="script"></div> 

とりあえず以上。

検索結果を表示したあと、表示をリセットする

Google Custom Searchのように表示結果を別ページに呼び出すオプションがないのでSPAだと他の画面に飛んでも表示されっぱなしになってしまう。簡易的な解決方法はページリロード。
ボタンをつけて、クリックするとリロードするようにした。
image.png

HTML

  <table style="border: white; border:0; width: 100%;">
    <tr>
      <td style="width: 95%">
        <div id="script"></div>  
      </td>
      <td style="width: 5%; vertical-align: top;">
        <button style="margin-left: 0.5em;" class="simple_square_btn2" (click)="reset()">
            表示<br>リセット
        </button>
      </td>
    </tr>
  </table>

CSS

.simple_square_btn2 {
    display: block;
    position: relative;
    width: 50px;
    height: 46px;
    padding: 0.3em;
    text-align: center;
    font-size: x-small;
    text-decoration: none;
    color: #1B1B1B;
    background: #fff;
    border:1px solid #1B1B1B;
}

TS

    // サーチクリア関数(リロードするだけ)
    reset() {
      location.reload();
    }


Appendix:Angularでエンドポイントを呼び出すコード

カスタマイズしたい場合のコードはAngularの場合、いかのような感じだろうか?テストしてないので動かないと思う。あくまでイメージ。

TS

import { Component, OnInit } from '@angular/core';
import { Observable } from  "rxjs";
import { HttpClient,HttpHeaders,HttpParams } from '@angular/common/http';

class bing {
  name: string;
  url: string;
  displayUrl; string;
  snippet: string;
  dateLastCrawled: string;
}

@Component({
  selector: 'app-bing',
  templateUrl: './bing.component.html',
  styleUrls: ['./bing.component.css']
})

export class BingComponent implements OnInit {

  bingObservable: Observable<bing[]>;

  subscriptionKey = 'a053xxxxxxxxxxxxxxxxxxxxf4ac'; //'YOUR-SUBSCRIPTION-KEY'
  customConfigId = 'fcbxxxx3-xxxx-xxxx-9676-ee7xxxxx516c2'; //'YOUR-CUSTOM-CONFIG-ID'; 
  searchTerm = '';

  url = 'https://bij.cognitiveservices.azure.com/bingcustomsearch/v7.0/search?' + 
  'q=' + this.searchTerm + '&' + 'customconfig=' + this.customConfigId + '&' + 'mkt=ja-JP';

  options = {
    headers: new HttpHeaders().append("Ocp-Apim-Subscription-Key", this.subscriptionKey),
    params: new HttpParams().append("url", this.url)
  }

  loadBing() {
    this.bingObservable = this.http.get<bing[]>(this.url, this.options);
    console.log('' + this.bingObservable)
  }

  constructor() {
  }

  ngOnInit() {
  }

HTML

<ul *ngIf="bingObservable| async as results else empty">
  <li *ngFor="let result of results">
  </li>
</ul>



参照:
Angular 9/8 Http - How to Use HttpClient Module with Examples
Angularのテンプレート内で script タグを読み込む方法
how to use bing search in my angular app.?

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5