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

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

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

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

mvn package -DskipTests

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

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

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

  1. กลับไปยัง Google Cloud Console
  2. ในการนำทางด้านซ้าย ให้คลิกข้อมูลเข้าสู่ระบบ ข้อมูล "เอกสารรับรอง" จะปรากฏขึ้น
  3. คลิกรายการแบบเลื่อนลง + สร้างข้อมูลรับรอง และเลือก บัญชีบริการ การตั้งค่า "สร้างบัญชีบริการ" จะปรากฏขึ้น
  4. ในช่องชื่อบัญชีบริการ ให้ป้อน "บทแนะนำ"
  5. สังเกตค่ารหัสบัญชีบริการ (อยู่หลังชื่อบัญชีบริการ) ค่านี้จะใช้ในภายหลัง
  6. คลิกสร้าง "สิทธิ์ของบัญชีบริการ (ไม่บังคับ)" จะปรากฏขึ้น
  7. คลิกดำเนินการต่อ สิทธิ์ "ให้สิทธิ์ผู้ใช้เข้าถึงบัญชีบริการนี้ (ไม่บังคับ)" จะปรากฏขึ้น
  8. คลิกเสร็จ ข้อมูล "ข้อมูลเข้าสู่ระบบ" หน้าจอจะปรากฏขึ้น
  9. คลิกอีเมลของบัญชีบริการในส่วนบัญชีบริการ "บริการ" รายละเอียดบัญชี" ผู้ดูหน้าเว็บ
  10. ภายใต้คีย์ ให้คลิกรายการแบบเลื่อนลงเพิ่มคีย์ แล้วเลือก สร้างคีย์ใหม่ การตั้งค่า "สร้างคีย์ส่วนตัว" จะปรากฏขึ้น
  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();
}

จากนั้นจึงทำให้อินเทอร์เฟซการค้นหาใช้งานได้

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