ทําให้เครื่องมือเชื่อมต่อใช้งานได้

หน้าของบทแนะนำ Cloud Search นี้จะแสดงวิธีตั้งค่าแหล่งข้อมูลและเครื่องมือเชื่อมต่อเนื้อหาสำหรับการจัดทำดัชนีข้อมูล หากต้องการเริ่มต้นตั้งแต่ตอนต้นของบทแนะนำนี้ โปรดดูบทแนะนำการเริ่มต้นใช้งาน Cloud Search

สร้างเครื่องมือเชื่อมต่อ

เปลี่ยนไดเรกทอรีการทำงานเป็นไดเรกทอรี cloud-search-samples/end-to-end/connector แล้วเรียกใช้คำสั่งนี้

mvn package -DskipTests

คำสั่งจะดาวน์โหลดทรัพยากร Dependency ที่จำเป็นสำหรับการสร้างเครื่องมือเชื่อมต่อเนื้อหาและคอมไพล์โค้ด

สร้างข้อมูลเข้าสู่ระบบบัญชีบริการ

เครื่องมือเชื่อมต่อต้องใช้ข้อมูลเข้าสู่ระบบบัญชีบริการเพื่อเรียกใช้ Cloud Search API วิธีสร้างข้อมูลเข้าสู่ระบบ

  1. กลับไปที่คอนโซล Google Cloud
  2. คลิกข้อมูลเข้าสู่ระบบที่การนำทางด้านซ้าย หน้า "ข้อมูลเข้าสู่ระบบ" จะปรากฏขึ้น
  3. คลิกรายการแบบเลื่อนลง + สร้างข้อมูลเข้าสู่ระบบ แล้วเลือกบัญชีบริการ หน้า "สร้างบัญชีบริการ" จะปรากฏขึ้น
  4. ป้อน "บทแนะนำ" ในช่องชื่อบัญชีบริการ
  5. จดบันทึกค่ารหัสบัญชีบริการ (ต่อจากชื่อบัญชีบริการ) ระบบจะใช้ค่านี้ในภายหลัง
  6. คลิกสร้าง กล่องโต้ตอบ "สิทธิ์ของบัญชีบริการ (ไม่บังคับ)" จะปรากฏขึ้น
  7. คลิกดำเนินการต่อ กล่องโต้ตอบ "ให้สิทธิ์ผู้ใช้เข้าถึงบัญชีบริการนี้ (ไม่บังคับ)" จะปรากฏขึ้น
  8. คลิกเสร็จสิ้น หน้าจอ "ข้อมูลเข้าสู่ระบบ" จะปรากฏขึ้น
  9. คลิกอีเมลบัญชีบริการในส่วนบัญชีบริการ หน้า "รายละเอียดบัญชีบริการ"
  10. ใต้คีย์ ให้คลิกรายการแบบเลื่อนลง ADD KEY แล้วเลือก Create new key (สร้างคีย์ใหม่) กล่องโต้ตอบ "สร้างคีย์ส่วนตัว" จะปรากฏขึ้น
  11. คลิกสร้าง
  12. (ไม่บังคับ) หากกล่องโต้ตอบ "คุณต้องการอนุญาตให้ดาวน์โหลดใน console.cloud.google.com ไหม" ปรากฏขึ้น ให้คลิกอนุญาต
  13. ระบบจะบันทึกไฟล์คีย์ส่วนตัวลงในคอมพิวเตอร์ จดตำแหน่งของไฟล์ที่ดาวน์โหลด ไฟล์นี้ใช้เพื่อกำหนดค่าเครื่องมือเชื่อมต่อเนื้อหาเพื่อให้ตรวจสอบสิทธิ์ตนเองได้เมื่อเรียกใช้ Google Cloud Search API

เริ่มต้นการสนับสนุนของบุคคลที่สาม

ก่อนที่จะเรียกใช้ Cloud Search API อื่นๆ ได้ คุณต้องเริ่มต้นการสนับสนุนของบุคคลที่สามสำหรับ Google Cloud Search

วิธีเริ่มต้นการสนับสนุนบุคคลที่สามสำหรับ Cloud Search

  1. โปรเจ็กต์แพลตฟอร์ม Cloud Search ของคุณมีข้อมูลเข้าสู่ระบบบัญชีบริการ อย่างไรก็ตาม คุณต้องสร้างข้อมูลเข้าสู่ระบบเว็บแอปพลิเคชันเพื่อช่วยในการเริ่มต้นการสนับสนุนของบุคคลที่สาม ดูวิธีสร้างข้อมูลเข้าสู่ระบบเว็บแอปพลิเคชันได้ที่สร้างข้อมูลเข้าสู่ระบบ คุณควรมีรหัสไคลเอ็นต์และไฟล์รหัสลับไคลเอ็นต์เมื่อเสร็จสิ้นขั้นตอนนี้

  2. ใช้สนามเด็กเล่น OAuth 2 ของ Google เพื่อรับโทเค็นเพื่อการเข้าถึง ดังนี้

    1. คลิกการตั้งค่าและทำเครื่องหมายที่ใช้ข้อมูลเข้าสู่ระบบในการตรวจสอบสิทธิ์ของคุณเอง
    2. ป้อนรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์จากขั้นตอนที่ 1
    3. คลิกปิด
    4. ในช่องขอบเขต ให้พิมพ์ https://www.googleapis.com/auth/cloud_search.settings แล้วคลิกให้สิทธิ์ สนามเด็กเล่น OAuth 2 จะแสดงรหัสการให้สิทธิ์
    5. คลิกรหัสการให้สิทธิ์ของ Exchange สำหรับโทเค็น ระบบจะส่งโทเค็นกลับมา
  3. หากต้องการเริ่มต้นการสนับสนุนของบุคคลที่สามสำหรับ Cloud Search ให้ใช้คำสั่ง curl ต่อไปนี้ อย่าลืมแทนที่ [YOUR_ACCESS_TOKEN] ด้วยโทเค็นที่ได้ในขั้นตอนที่ 2

    curl --request POST \
    'https://cloudsearch.googleapis.com/v1:initializeCustomer' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/json' \
      --data '{}' \
      --compressed
    

    หากทำสำเร็จ เนื้อหาการตอบกลับจะมีอินสแตนซ์ operation เช่น

    {
    name: "operations/customers/01b3fqdm/lro/AOIL6eBv7fEfiZ_hUSpm8KQDt1Mnd6dj5Ru3MXf-jri4xK6Pyb2-Lwfn8vQKg74pgxlxjrY"
    }
    

    หากไม่สำเร็จ โปรดติดต่อทีมสนับสนุนของ Cloud Search

  4. ใช้ operations.get เพื่อยืนยันว่ามีเริ่มต้นการสนับสนุนบุคคลที่สามแล้ว

    curl \
    'https://cloudsearch.googleapis.com/v1/operations/customers/01b3fqdm/lro/AOIL6eBv7fEfiZ_hUSpm8KQDt1Mnd6dj5Ru3MXf-jri4xK6Pyb2-Lwfn8vQKg74pgxlxjrY?key=
    [YOUR_API_KEY]' \
    --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
    --header 'Accept: application/json' \
    --compressed
    

    เมื่อการเริ่มต้นของบุคคลที่สามเสร็จสมบูรณ์แล้ว จะมีช่อง done ที่ตั้งค่าเป็น true เช่น

    {
    name: "operations/customers/01b3fqdm/lro/AOIL6eBv7fEfiZ_hUSpm8KQDt1Mnd6dj5Ru3MXf-jri4xK6Pyb2-Lwfn8vQKg74pgxlxjrY"
    done: true
    }
    

สร้างแหล่งข้อมูล

จากนั้นสร้างแหล่งข้อมูลในคอนโซลผู้ดูแลระบบ แหล่งข้อมูลจะมีเนมสเปซสำหรับการจัดทำดัชนีเนื้อหาโดยใช้เครื่องมือเชื่อมต่อ

  1. เปิดคอนโซลผู้ดูแลระบบของ Google
  2. คลิกไอคอน "แอป" หน้า "การดูแลระบบแอป" จะปรากฏขึ้น
  3. คลิก Google Workspace หน้า "การดูแลระบบ Google Workspace ของแอป" จะปรากฏขึ้น
  4. เลื่อนลงและคลิก Cloud Search หน้า "การตั้งค่าสำหรับ Google Workspace" จะปรากฏขึ้น
  5. คลิกแหล่งข้อมูลจากบุคคลที่สาม หน้า "แหล่งข้อมูล" จะปรากฏขึ้น
  6. คลิกวงกลมสีเหลือง + กล่องโต้ตอบ "เพิ่มแหล่งข้อมูลใหม่" จะปรากฏขึ้น
  7. พิมพ์ "บทแนะนำ" ในช่องชื่อที่แสดง
  8. ในช่องอีเมลบัญชีบริการ ให้ป้อนอีเมลของบัญชีบริการที่สร้างไว้ในส่วนก่อนหน้า หากไม่ทราบอีเมลของบัญชีบริการ ให้ดูค่าในหน้าบัญชีบริการ
  9. คลิกเพิ่ม กล่องโต้ตอบ "สร้างแหล่งข้อมูลเรียบร้อยแล้ว" จะปรากฏขึ้น
  10. คลิก *ตกลง โปรดจดรหัสแหล่งที่มาของแหล่งข้อมูลที่สร้างขึ้นใหม่ ระบบจะใช้รหัสแหล่งที่มาเพื่อกำหนดค่าเครื่องมือเชื่อมต่อเนื้อหา

สร้างโทเค็นเพื่อการเข้าถึงส่วนบุคคลสำหรับ GitHub API

เครื่องมือเชื่อมต่อต้องมีการตรวจสอบสิทธิ์การเข้าถึง GitHub API เพื่อให้มีโควต้าเพียงพอ เครื่องมือเชื่อมต่อจะใช้ประโยชน์จากโทเค็นเพื่อการเข้าถึงส่วนบุคคลแทน OAuth เพื่อความสะดวก โทเค็นส่วนตัวช่วยให้ตรวจสอบสิทธิ์ในฐานะผู้ใช้ที่มีชุดสิทธิ์จำกัดคล้ายกับ OAuth ได้

  1. ลงชื่อเข้าสู่ระบบ GitHub
  2. คลิกรูปโปรไฟล์ที่มุมบนขวา เมนูแบบเลื่อนลงจะปรากฏขึ้น
  3. คลิกการตั้งค่า
  4. คลิกการตั้งค่าสำหรับนักพัฒนาซอฟต์แวร์
  5. คลิกโทเค็นเพื่อการเข้าถึงส่วนบุคคล
  6. คลิกสร้างโทเค็นเพื่อการเข้าถึงส่วนบุคคล
  7. ในช่องหมายเหตุ ให้ป้อน "บทแนะนำ Cloud Search"
  8. ตรวจสอบขอบเขต public_repo
  9. คลิกสร้างโทเค็น
  10. จดโทเค็นที่สร้างขึ้น เครื่องมือเชื่อมต่อจะใช้โค้ดนี้เพื่อเรียกใช้ GitHub API และให้โควต้า API สำหรับการจัดทำดัชนี

กำหนดค่าเครื่องมือเชื่อมต่อ

หลังจากสร้างข้อมูลเข้าสู่ระบบและแหล่งข้อมูลแล้ว ให้อัปเดตการกำหนดค่าเครื่องมือเชื่อมต่อให้รวมค่าต่อไปนี้ด้วย

  1. จากบรรทัดคำสั่ง ให้เปลี่ยนไดเรกทอรีเป็น cloud-search-samples/end-to-end/connector/
  2. เปิดไฟล์ sample-config.properties ด้วยเครื่องมือแก้ไขข้อความ
  3. ตั้งค่าพารามิเตอร์ api.serviceAccountPrivateKeyFile เป็นเส้นทางไฟล์ของข้อมูลเข้าสู่ระบบบริการที่คุณดาวน์โหลดไว้ก่อนหน้านี้
  4. ตั้งค่าพารามิเตอร์ api.sourceId เป็นรหัสของแหล่งข้อมูลที่สร้างไว้ก่อนหน้านี้
  5. ตั้งค่าพารามิเตอร์ github.user เป็นชื่อผู้ใช้ GitHub
  6. ตั้งค่าพารามิเตอร์ github.token เป็นโทเค็นเพื่อการเข้าถึงที่สร้างไว้ก่อนหน้านี้
  7. บันทึกไฟล์

อัปเดตสคีมา

เครื่องมือเชื่อมต่อจะจัดทำดัชนีทั้งเนื้อหาแบบมีโครงสร้างและไม่มีโครงสร้าง ก่อนที่จะจัดทำดัชนีข้อมูล คุณต้องอัปเดตสคีมาสำหรับแหล่งข้อมูล เรียกใช้คำสั่งต่อไปนี้เพื่ออัปเดตสคีมา

mvn exec:java -Dexec.mainClass=com.google.cloudsearch.tutorial.SchemaTool \
    -Dexec.args="-Dconfig=sample-config.properties"

เรียกใช้เครื่องมือเชื่อมต่อ

หากต้องการเรียกใช้เครื่องมือเชื่อมต่อและเริ่มการจัดทำดัชนี ให้เรียกใช้คำสั่งต่อไปนี้

mvn exec:java -Dexec.mainClass=com.google.cloudsearch.tutorial.GithubConnector \
    -Dexec.args="-Dconfig=sample-config.properties"

การกำหนดค่าเริ่มต้นสำหรับเครื่องมือเชื่อมต่อคือการจัดทำดัชนีที่เก็บรายการเดียวในองค์กร googleworkspace การจัดทำดัชนีที่เก็บจะใช้เวลาประมาณ 1 นาที หลังจากการจัดทำดัชนีเริ่มต้น เครื่องมือเชื่อมต่อจะยังคงสำรวจการเปลี่ยนแปลงที่เก็บซึ่งจำเป็นต้องปรากฏในดัชนี Cloud Search

การตรวจสอบโค้ด

ส่วนที่เหลือจะเป็นการตรวจสอบวิธีสร้างเครื่องมือเชื่อมต่อ

กำลังเริ่มต้นแอปพลิเคชัน

จุดแรกเข้าของเครื่องมือเชื่อมต่อคือคลาส GithubConnector เมธอด main จะสร้างอินสแตนซ์ IndexingApplication ของ SDK และเริ่มต้น

GithubConnector.java
/**
 * Main entry point for the connector. Creates and starts an indexing
 * application using the {@code ListingConnector} template and the sample's
 * custom {@code Repository} implementation.
 *
 * @param args program command line arguments
 * @throws InterruptedException thrown if an abort is issued during initialization
 */
public static void main(String[] args) throws InterruptedException {
  Repository repository = new GithubRepository();
  IndexingConnector connector = new ListingConnector(repository);
  IndexingApplication application = new IndexingApplication.Builder(connector, args)
      .build();
  application.start();
}

ListingConnector ที่ SDK จัดเตรียมไว้ให้จะใช้กลยุทธ์การส่งผ่านซึ่งใช้ประโยชน์จากคิวของ Cloud Search ในการติดตามสถานะของรายการในดัชนี โดยจะมอบสิทธิ์ให้ GithubRepository ซึ่ง ใช้โดยเครื่องมือเชื่อมต่อตัวอย่าง สำหรับการเข้าถึงเนื้อหาจาก GitHub

การสำรวจที่เก็บ GitHub

ในระหว่างการข้ามผ่านเต็มรูปแบบ ระบบจะเรียกใช้เมธอด getIds() เพื่อพุชรายการที่อาจต้องจัดทำดัชนีไปยังคิว

เครื่องมือเชื่อมต่อสามารถจัดทำดัชนีที่เก็บหรือองค์กรหลายรายการได้ จะมีการข้ามผ่านที่เก็บ GitHub ทีละ 1 รายการเพื่อลดผลกระทบจากความล้มเหลว จุดตรวจสอบจะถูกแสดงผลพร้อมกับผลลัพธ์ของการข้ามผ่านที่มีรายการของที่เก็บที่จะทำดัชนีในการเรียก getIds() ที่ตามมา หากเกิดข้อผิดพลาด การจัดทำดัชนีจะดำเนินการต่อที่ที่เก็บปัจจุบันแทนที่จะเริ่มต้นตั้งแต่แรก

GithubRepository.java
/**
 * Gets all of the existing item IDs from the data repository. While
 * multiple repositories are supported, only one repository is traversed
 * per call. The remaining repositories are saved in the checkpoint
 * are traversed on subsequent calls. This minimizes the amount of
 * data that needs to be reindex in the event of an error.
 *
 * <p>This method is called by {@link ListingConnector#traverse()} during
 * <em>full traversals</em>. Every document ID and metadata hash value in
 * the <em>repository</em> is pushed to the Cloud Search queue. Each pushed
 * document is later polled and processed in the {@link #getDoc(Item)} method.
 * <p>
 * The metadata hash values are pushed to aid document change detection. The
 * queue sets the document status depending on the hash comparison. If the
 * pushed ID doesn't yet exist in Cloud Search, the document's status is
 * set to <em>new</em>. If the ID exists but has a mismatched hash value,
 * its status is set to <em>modified</em>. If the ID exists and matches
 * the hash value, its status is unchanged.
 *
 * <p>In every case, the pushed content hash value is only used for
 * comparison. The hash value is only set in the queue during an
 * update (see {@link #getDoc(Item)}).
 *
 * @param checkpoint value defined and maintained by this connector
 * @return this is typically a {@link PushItems} instance
 */
@Override
public CheckpointCloseableIterable<ApiOperation> getIds(byte[] checkpoint)
    throws RepositoryException {
  List<String> repositories;
  // Decode the checkpoint if present to get the list of remaining
  // repositories to index.
  if (checkpoint != null) {
    try {
      FullTraversalCheckpoint decodedCheckpoint = FullTraversalCheckpoint
          .fromBytes(checkpoint);
      repositories = decodedCheckpoint.getRemainingRepositories();
    } catch (IOException e) {
      throw new RepositoryException.Builder()
          .setErrorMessage("Unable to deserialize checkpoint")
          .setCause(e)
          .build();
    }
  } else {
    // No previous checkpoint, scan for repositories to index
    // based on the connector configuration.
    try {
      repositories = scanRepositories();
    } catch (IOException e) {
      throw toRepositoryError(e, Optional.of("Unable to scan repositories"));
    }
  }

  if (repositories.isEmpty()) {
    // Nothing left to index. Reset the checkpoint to null so the
    // next full traversal starts from the beginning
    Collection<ApiOperation> empty = Collections.emptyList();
    return new CheckpointCloseableIterableImpl.Builder<>(empty)
        .setCheckpoint((byte[]) null)
        .setHasMore(false)
        .build();
  }

  // Still have more repositories to index. Pop the next repository to
  // index off the list. The remaining repositories make up the next
  // checkpoint.
  String repositoryToIndex = repositories.get(0);
  repositories = repositories.subList(1, repositories.size());

  try {
    log.info(() -> String.format("Traversing repository %s", repositoryToIndex));
    Collection<ApiOperation> items = collectRepositoryItems(repositoryToIndex);
    FullTraversalCheckpoint newCheckpoint = new FullTraversalCheckpoint(repositories);
    return new CheckpointCloseableIterableImpl.Builder<>(items)
        .setHasMore(true)
        .setCheckpoint(newCheckpoint.toBytes())
        .build();
  } catch (IOException e) {
    String errorMessage = String.format("Unable to traverse repo: %s",
        repositoryToIndex);
    throw toRepositoryError(e, Optional.of(errorMessage));
  }
}

เมธอด collectRepositoryItems() จะจัดการการส่งผ่านของที่เก็บ GitHub เดียว เมธอดนี้จะแสดงคอลเล็กชัน ApiOperations ที่แสดงถึงรายการที่จะพุชลงในคิว รายการจะพุชเป็นชื่อทรัพยากรและค่าแฮชที่แสดงสถานะปัจจุบันของรายการ

ระบบจะใช้ค่าแฮชในการข้ามผ่านที่เก็บ GitHub ครั้งต่อๆ ไป ค่านี้เป็นการตรวจสอบง่ายๆ เพื่อตัดสินว่าเนื้อหามีการเปลี่ยนแปลงหรือไม่โดยไม่ต้องอัปโหลดเนื้อหาเพิ่มเติม เครื่องมือเชื่อมต่อจะ จัดคิวรายการทั้งหมด หากรายการเป็นค่าใหม่หรือค่าแฮชมีการเปลี่ยนแปลง รายการนั้นจะพร้อมใช้งานสำหรับการหยั่งสัญญาณในคิว มิฉะนั้นจะถือว่ารายการไม่มีการแก้ไข

GithubRepository.java
/**
 * Fetch IDs to  push in to the queue for all items in the repository.
 * Currently captures issues & content in the master branch.
 *
 * @param name Name of repository to index
 * @return Items to push into the queue for later indexing
 * @throws IOException if error reading issues
 */
private Collection<ApiOperation> collectRepositoryItems(String name)
    throws IOException {
  List<ApiOperation> operations = new ArrayList<>();
  GHRepository repo = github.getRepository(name);

  // Add the repository as an item to be indexed
  String metadataHash = repo.getUpdatedAt().toString();
  String resourceName = repo.getHtmlUrl().getPath();
  PushItem repositoryPushItem = new PushItem()
      .setMetadataHash(metadataHash);
  PushItems items = new PushItems.Builder()
      .addPushItem(resourceName, repositoryPushItem)
      .build();

  operations.add(items);
  // Add issues/pull requests & files
  operations.add(collectIssues(repo));
  operations.add(collectContent(repo));
  return operations;
}

กำลังประมวลผลคิว

หลังจากการข้ามผ่านทั้งหมดเสร็จสมบูรณ์ เครื่องมือเชื่อมต่อจะเริ่มสำรวจคิวรายการที่ต้องจัดทำดัชนี ระบบจะเรียกใช้เมธอด getDoc() สำหรับแต่ละรายการที่ดึงมาจากคิว เมธอดจะอ่านรายการจาก GitHub และแปลงเป็นตัวแทนที่เหมาะสมสำหรับการจัดทำดัชนี

เนื่องจากเครื่องมือเชื่อมต่อกำลังทำงานกับข้อมูลจริงที่อาจมีการเปลี่ยนแปลงได้ตลอดเวลา getDoc() จะตรวจสอบว่ารายการในคิวยังคงใช้งานได้อยู่และจะลบรายการใดๆ ออกจากดัชนีที่ไม่มีอยู่แล้ว

GithubRepository.java
/**
 * Gets a single data repository item and indexes it if required.
 *
 * <p>This method is called by the {@link ListingConnector} during a poll
 * of the Cloud Search queue. Each queued item is processed
 * individually depending on its state in the data repository.
 *
 * @param item the data repository item to retrieve
 * @return the item's state determines which type of
 * {@link ApiOperation} is returned:
 * {@link RepositoryDoc}, {@link DeleteItem}, or {@link PushItem}
 */
@Override
public ApiOperation getDoc(Item item) throws RepositoryException {
  log.info(() -> String.format("Processing item: %s ", item.getName()));
  Object githubObject;
  try {
    // Retrieve the item from GitHub
    githubObject = getGithubObject(item.getName());
    if (githubObject instanceof GHRepository) {
      return indexItem((GHRepository) githubObject, item);
    } else if (githubObject instanceof GHPullRequest) {
      return indexItem((GHPullRequest) githubObject, item);
    } else if (githubObject instanceof GHIssue) {
      return indexItem((GHIssue) githubObject, item);
    } else if (githubObject instanceof GHContent) {
      return indexItem((GHContent) githubObject, item);
    } else {
      String errorMessage = String.format("Unexpected item received: %s",
          item.getName());
      throw new RepositoryException.Builder()
          .setErrorMessage(errorMessage)
          .setErrorType(RepositoryException.ErrorType.UNKNOWN)
          .build();
    }
  } catch (FileNotFoundException e) {
    log.info(() -> String.format("Deleting item: %s ", item.getName()));
    return ApiOperations.deleteItem(item.getName());
  } catch (IOException e) {
    String errorMessage = String.format("Unable to retrieve item: %s",
        item.getName());
    throw toRepositoryError(e, Optional.of(errorMessage));
  }
}

สำหรับออบเจ็กต์ GitHub แต่ละรายการที่เครื่องมือเชื่อมต่อจัดทำดัชนี เมธอด indexItem() ที่เกี่ยวข้องจะจัดการการสร้างการนำเสนอรายการสำหรับ Cloud Search ตัวอย่างเช่น หากต้องการสร้างตัวแทนสำหรับรายการเนื้อหา ให้ทำดังนี้

GithubRepository.java
/**
 * Build the ApiOperation to index a content item (file).
 *
 * @param content      Content item to index
 * @param previousItem Previous item state in the index
 * @return ApiOperation (RepositoryDoc if indexing,  PushItem if not modified)
 * @throws IOException if unable to create operation
 */
private ApiOperation indexItem(GHContent content, Item previousItem)
    throws IOException {
  String metadataHash = content.getSha();

  // If previously indexed and unchanged, just requeue as unmodified
  if (canSkipIndexing(previousItem, metadataHash)) {
    return notModified(previousItem.getName());
  }

  String resourceName = new URL(content.getHtmlUrl()).getPath();
  FieldOrValue<String> title = FieldOrValue.withValue(content.getName());
  FieldOrValue<String> url = FieldOrValue.withValue(content.getHtmlUrl());

  String containerName = content.getOwner().getHtmlUrl().getPath();
  String programmingLanguage = FileExtensions.getLanguageForFile(content.getName());

  // Structured data based on the schema
  Multimap<String, Object> structuredData = ArrayListMultimap.create();
  structuredData.put("organization", content.getOwner().getOwnerName());
  structuredData.put("repository", content.getOwner().getName());
  structuredData.put("path", content.getPath());
  structuredData.put("language", programmingLanguage);

  Item item = IndexingItemBuilder.fromConfiguration(resourceName)
      .setTitle(title)
      .setContainerName(containerName)
      .setSourceRepositoryUrl(url)
      .setItemType(IndexingItemBuilder.ItemType.CONTAINER_ITEM)
      .setObjectType("file")
      .setValues(structuredData)
      .setVersion(Longs.toByteArray(System.currentTimeMillis()))
      .setHash(content.getSha())
      .build();

  // Index the file content too
  String mimeType = FileTypeMap.getDefaultFileTypeMap()
      .getContentType(content.getName());
  AbstractInputStreamContent fileContent = new InputStreamContent(
      mimeType, content.read())
      .setLength(content.getSize())
      .setCloseInputStream(true);
  return new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(fileContent, IndexingService.ContentFormat.RAW)
      .setRequestMode(IndexingService.RequestMode.SYNCHRONOUS)
      .build();
}

ต่อไป ให้ทำให้อินเทอร์เฟซการค้นหาใช้งานได้

ก่อนหน้า ถัดไป