![](/img/trans.png)
[英]Is it possible to create an index on a multi nested field on cosmos mongo db?
[英]Unable to map nested datasource field of cosmos db to a root index field of Azure indexer using REST APIs
我有一個具有以下數據格式的 mongo db 集合users
{
"name": "abc",
"email": "abc@xyz.com"
"address": {
"city": "Gurgaon",
"state": "Haryana"
}
}
現在我正在使用 azure rest api 為這個集合創建一個數據源、一個索引和一個索引器。
數據源
def create_datasource():
request_body = {
"name": 'users-datasource',
"description": "",
"type": "cosmosdb",
"credentials": {
"connectionString": "<db conenction url>"
},
"container": {"name": "users"},
"dataChangeDetectionPolicy": {
"@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy",
"highWaterMarkColumnName": "_ts"
}
}
resp = requests.post(url="<create-datasource-api-url>", data=json.dumps(request_body),
headers=headers)
上述數據源的索引
def create_index(config):
request_body = {
'name': "users-index",
'fields': [
{
'name': 'name',
'type': 'Edm.String'
},
{
'name': 'email',
'type': 'Edm.DateTimeOffset'
},
{
'name': 'address',
'type': 'Edm.String'
},
{
'name': 'doc_id',
'type': 'Edm.String',
'key': True
}
]
}
resp = requests.post(url="<azure-create-index-api-url>", data=json.dumps(request_body),
headers=config.headers)
現在是上述數據源和索引的索引器
def create_interviews_indexer(config):
request_body = {
"name": "users-indexer",
"dataSourceName": "users-datasource",
"targetIndexName": users-index,
"schedule": {"interval": "PT5M"},
"fieldMappings": [
{"sourceFieldName": "address.city", "targetFieldName": "address"},
]
}
resp = requests.post("create-indexer-pi-url", data=json.dumps(request_body),
headers=config.headers)
這會毫無例外地創建索引器,但是當我在 azure 門戶中為users-indexer
檢查檢索到的數據時, address
字段是null
並且沒有從創建索引器時提供的address.city
字段映射中獲取任何值。
我也嘗試過以下代碼作為映射,但它也不起作用。
"fieldMappings": [
{"sourceFieldName": "/address/city", "targetFieldName": "address"},
]
azure 文檔也沒有說明這種映射。 因此,如果有人可以幫助我,將不勝感激。
數據源定義中的container
元素允許您指定可用於展平 JSON 文檔的query
(參考:https://docs.microsoft.com/en-us/rest/api/searchservice/create-data-source )因此,您可以編寫查詢並以所需格式獲取 output,而不是在索引器定義中進行列映射。
在這種情況下,您創建數據源的代碼將是:
def create_datasource():
request_body = {
"name": 'users-datasource',
"description": "",
"type": "cosmosdb",
"credentials": {
"connectionString": "<db conenction url>",
},
"container": {
"name": "users",
"query": "SELECT a.name, a.email, a.address.city as address FROM a",
},
"dataChangeDetectionPolicy": {
"@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy",
"highWaterMarkColumnName": "_ts"
}
}
resp = requests.post(url="<create-datasource-api-url>", data=json.dumps(request_body),
headers=headers)
對 MongoDb API 風格的支持是公共預覽版 - 您需要在數據源的連接字符串中明確指出 Mongo,如 本文所述。 另請注意,對於 Mongo 數據源,afaik 不支持先前響應建議的自定義查詢。 希望團隊中的某個人能夠澄清此支持的當前 state。
正確使用以下字段映射對我有用。 Azure 搜索查詢正在正確返回地址值。
"fieldMappings": [{"sourceFieldName": "address.city", "targetFieldName": "address"}]
我確實對您提供的數據進行了一些更改,例如
請確保您使用的是預覽版 API 版本,因為對於 MongoDB API 處於預覽模式,Z3A580F142089977F5FZFBC3。 例如 https://{azure search name}.search.windows.net/indexers?api-version= 2019-05-06-Preview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.