如何設定 Post SearchResult 介面為搜索回傳值
我無法設定介面獲取回傳結果你可以查看下面的代碼
依賴關系
"@nestjs/elasticsearch": "^8.1.0",
"@elastic/elasticsearch": "^8.2.1",
"@types/elasticsearch": "^5.0.40",
界面帖子
export interface PostSearchResultInterface {
hits: {
total: {
value: number;
};
hits: Array<{
total: {
value: number;
}
_source: PostSearchBodyInterface;
}>;
};
}
代碼
const search = await this.elasticsearchService.search<PostSearchBodyInterface>({
index: this.index,
from: offset,
size: limit,
body: {
query: {
bool: {
should: {
multi_match: {
query: text,
fields: ['title', 'story', 'other_titles']
}
},
filter: {
range: {
id: {
gte: startId
}
}
}
},
},
sort: {
id: {
order: 'asc'
}
}
}
}) ;
// error here
let count = search.hits.total.value
const hits = search.hits.hits;
const results = hits.map((item) => item._source);
return {
count : startId ? separateCount : count,
results
}
錯誤 TS2339:型別“數字”上不存在屬性“值”| SearchTotalHits'。型別“數字”上不存在屬性“值”。
ERROR: let count = search.hits.total.value
uj5u.com熱心網友回復:
假設依賴項是(如問題發布):
"@nestjs/elasticsearch": "^8.1.0",
"@elastic/elasticsearch": "^8.2.1",
"@types/elasticsearch": "^5.0.40",
首先,為索引檔案創建一個專用型別,例如:
export type Document = {
title: string,
tags: string[]
};
然后,只需搜索檔案:
const a = await this.elastic.search<Document>({ //...request... });
const total = a.hits.total;
const documents = a.hits.hits.map(document => {
// query metadata fields returned by elastic
document._id;
// get fields for <Document> type
document._source.title...
document._source.tags...
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/498423.html
標籤:javascript 节点.js 打字稿 弹性搜索 巢穴
上一篇:使用js在點擊時切換圖示符號
