簡體   English   中英

使用 elasticsearch firestore 和 react-native 進行全文搜索

[英]Doing full text search with elasticsearch firestore and react-native

我有一個關於如何在 firestore 上進行全文搜索的問題。 我有一個簡單的應用程序,它是我在 React Native 上編寫的。 我的應用程序運行良好,我有雲功能,可以觸發 onCreate、onEdit 等。

我想集成 Elastic Search。 根據firebase文檔和Elasticsearch文檔,沒有 package,我可以安裝它以直接從 react-native 應用程序連接到 Elasticsearch。 我需要編寫一個代理服務器(在 node.js 上),我需要連接到 firebase(這樣我的 firebase 函數將在我創建文檔時被調用,當我編輯它等......)我的 firebase 函數現在將做兩個事物:

  1. 當我創建文檔時他們現在所做的操作(例如增量計數); 這些是動作,它們來自我的 React Native 項目
  2. 他們還將數據復制到彈性搜索服務,以便我能夠進行全文搜索

我的理解正確嗎? 我的下一個問題是,如何通過我的 React Native 應用程序連接到 node.js 代理服務器? 我正在編寫一個移動應用程序,我想將它發布到 google play 商店。 當它依賴於代理服務器(寫在 node.js 上)時,我如何發布它? 我還需要在其他地方發布代理服務器嗎?

基本上,我很難理解我的本機反應應用程序將如何調用代理服務器。

編輯:我認為將此鏈接作為參考是個好主意——基本上我的出發點是安裝@elastic/elasticsearch package 並直接從我的本機反應應用程序使用它。 我遇到了很多問題,經過一番挖掘,我在這里找到了一個討論,人們解釋了為什么你不能將@elastic/elasticsearch package 與 react native 一起使用,他們建議使用代理服務器。

因此,為什么我要詢問有關將我的應用程序連接到代理服務器的問題。 正如我在評論中所寫(根據我的理解),該代理服務器將提供兩個功能:

  1. 我所有的雲功能(現在位於我的 React Nativ 項目中)都將移至此代理服務器中,以便服務器將偵聽onCreateonEdit等更改,...如果調用 function,服務器將我的數據與 elasticsearch 同步
  2. 服務器將為我提供/search/端點,我將從我的 React 本機應用程序調用這些端點。

我的理解正確嗎?

注意:雖然此答案完全基於 Firebase 文檔,但建議使用相應的Search with Elastic App Search Firebase Extension 而不是自己實施。 該擴展將幫助您更快地部署此功能,因為它是一個預打包的解決方案。

如何通過我的 React Native 應用程序連接到 node.js 代理服務器? 我正在編寫一個移動應用程序,我想將它發布到 google play 商店。 當它依賴於代理服務器(寫在 node.js 上)時,我如何發布它? 我還需要在其他地方發布代理服務器嗎?

您參考的Firebase文檔中的示例所示,有兩個Cloud Functions要實現(我復制下面的示例代碼以供參考。):

  1. onNoteCreated Cloud Function 每次寫博文時觸發,調用Elastic Search更新搜索索引
  2. searchNotes Cloud Function 是從您的應用程序調用的Callable Cloud Function ,它調用 Elastic Search 來搜索索引並將結果返回給您的應用程序

請注意,文檔中包含的示例中標記為“Node.js”的所有塊(一個筆記應用程序,其中每個筆記都是一個文檔)將包含在您的 Cloud Function index.js文件中。 您可以很好地將自己的當前代碼添加到onCreate Firestore 觸發的 Cloud Function。

換句話說,您在問題中所說的“代理服務器”可以在 Cloud Functions 中完全實現,實際上不需要管理和擴展您自己的服務器。

const functions = require("firebase-functions");

const { Client } = require("@elastic/elasticsearch");

// Initialize Elastic, requires installing Elastic dependencies:
// https://github.com/elastic/elasticsearch-js
//
// ID, username, and password are stored in functions config variables
const ELASTIC_ID = functions.config().elastic.id;
const ELASTIC_USERNAME = functions.config().elastic.username;
const ELASTIC_PASSWORD = functions.config().elastic.password;

const client = new Client({
  cloud: {
    id: ELASTIC_ID,
    username: ELASTIC_USERNAME,
    password: ELASTIC_PASSWORD,
  }
});


// Update the search index every time a blog post is written.
exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(async (snap, context) => {
  // Get the note document
  const note = snap.data();

  // Use the 'nodeId' path segment as the identifier for Elastic
  const id = context.params.noteId;

  // Write to the Elastic index
  client.index({
    index: "notes",
    id,
    body: note,
  });
});

exports.searchNotes = functions.https.onCall(async (data, context) => {
  const query = data.query;

  // Search for any notes where the text field contains the query text.
  // For more search examples see:
  // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html
  const searchRes = await client.search({
    index: "notes",
    body: {
      query: {
        query_string: {
          query: `*${query}*`,
          fields: [
            "text"
          ]
        }
      }
    }
  });

  // Each entry will have the following properties:
  //   _score: a score for how well the item matches the search
  //   _source: the original item data
  const hits = searchRes.body.hits.hits;

  const notes = hits.map(h => h["_source"]);
  return {
    notes: notes
  };
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM