DQL 概觀

Discovery 查詢語言定義了您可以用來篩選、搜尋和分析資料的語法。

如何撰寫 Discovery 查詢語言查詢

Discovery 查詢語言利用索引文件的結構。 下面的 JSON 片段顯示了一個應用了 Entities 豐富功能的資料集中的索引文件。 經過豐富後,JSON 結構會捕捉任何已知實體的提述,例如城市名稱、公司或名人。

在這個範例中,辨識的實體是公司名稱 IBM。

{
  "document": {
    "document_id": "f7f27ea30eb3e4c0ce21830618d9ee99",
    "enriched_text": [
      {
        "entities": [
          {
            "model_name": "natural_language_understanding",
            "mentions": [],
            "text":"IBM",
            "type":"Organization"
          }
        ]
      }
    ]
  }
}

若要建立查詢,以傳回所有提及實體 IBM 的文件,請使用下列語法:

The structure of the query enriched_text.entities.text:IBM, where text in enriched_text is the field where the enrichment is applied, and IBM is the term you are looking for in the enriched_text.entities.text subfield.
查詢結構範例

此基本查詢在 : 操作符之前包含一個巢狀路徑表達式。 每個路徑元素都是文件中一個欄位的名稱,以句點 (.)分隔。: 運算符表示運算符後面的文字必須包含在結果中。

:: 運算符號表示文字必須在結果中完全匹配。 如需詳細資訊,請參閱 查詢操作符。 您可以在以下範例中看到這兩個運算符號的使用方式。

  • 若要按相關性順序傳回匹配的文件,請在 POST 請求中傳入下列資料物件:

    {
      "query":"enriched_text.entities.text:IBM"
    }
    
  • 若要以任何順序傳回匹配的文件,請在 POST 請求中傳入下列資料物件作為查詢正文:

    {
      "filter":"enriched_text.entities.text::IBM"
    }
    

同時使用篩選器和查詢參數

The filter parameter returns faster than the query parameter and its results are cached. If you submit queries that use the filter and query parameters separately on a small data set, each request returns similar (if not identical) results.

In large data sets, if you need results to be returned in order of relevance, combine the filter and query parameters. 同時使用這些參數可改善效能,因為 filter 參數會先套用。 它會過濾文件並快取結果。 query 參數會對快取結果進行排序。

篩選器範例:依 ID 取得文件

查詢主體:

{
  "filter": "document_id::b6d8c6e3-1097-421b-9e39-75717d2554aa"
}

如果該文件存在,查詢會傳回 1 個符合的結果。 如果沒有,查詢就不會傳回匹配的結果。

篩選器範例:依文件名尋找文件 ID

If you don't know the document_id of a document, but you know the original filename of the document, you can use the filter and return parameters together to discover the document_id.

查詢主體:

{
  "filter": "extracted_metadata.filename::100674.txt",
  "return": [ "document_id", "extracted_metadata" ]
}

回應:

{
  "matching_results": 1,
  "results": [
    {
      "document_id": "b6d8c6e3-1097-421b-9e39-75717d2554aa",
      "extracted_metadata": {
        "sha1": "AD447F7592A17CDCBF0A589C4E6EC2087AF7H35F",
        "filename": "100674.txt",
        "file_type": "text"
      }
    }
  ]
}

篩選器範例:尋找提及實體值的文件

查詢會尋找提及實體 Gilroy 的文件,並找到 4 個符合的文件。

查詢主體:

{
  "filter": "enriched_text.entities.text::Gilroy"
}

回應:

{
  "matching_results": 4
}

篩選嵌套值

您可以在一個篩選條件內嵌套另一個篩選條件,以確保返回的文件符合多個條件。

In the documents used for these examples, the entity "Gilroy" appears as both a "Location" (a town in California) and as a "Person" (a surname) entity type. To find documents where "Gilroy" appears as a location, write a query that filters on two nested fields at the same time: the entity text must be "Gilroy" and the entity type must be "Location".

查詢的一種寫法如下:

{
  "filter": "enriched_text.entities.text::Gilroy,enriched_text.entities.type::Location"
}

This query matches documents where some path enriched_text.entities.text is Gilroy and some path enriched_text.entities.type::Location is Location. 但是,無法保證這兩個路徑會在同一個 entities 物件之下。 For example, the query matches documents that have Gilroy as a Person entity type and, at the same time, have some other Location entity type object.

為了準確捕捉此查詢的巢狀語意,請使用下列語法來巢狀篩選值:

查詢主體:

{
  "filter": "enriched_text.entities:(text::Gilroy,type::Location)"
}

This stricter query matches only those documents in which there is an entities object with text equal to Gilroy and type equal to Location.

As another example, if you want to match documents that contain an entities object with text equal to Gilroy but type 不是 equal to Location, you can use the 不等 operator in the query, for example:

{
  "filter": "enriched_text.entities:(text::Gilroy,type::!Location)"
}

您也可以使用聚合來對結果進行更精密的篩選。 有關可用聚合類型的詳細資訊,請參閱 查詢聚合。

如需 Discovery Query Language 的詳細資訊,請參閱下列主題: