我是MarkLogic的新手。我正在寻找一种仅在使用jsearch时从文档输出匹配元素的方法,但是在执行查询时,搜索到的术语的元素与匹配是未知的。让我来说明一下:
jsearch.documents()
.where(cts.wordQuery('mark'))
.result();
这给了我所有包含'标记'的文件,例如
{
"results": [
{
"index": 0,
"uri": "/books/twain4.json",
"score": 14336,
"confidence": 0.432453483343124,
"fitness": 0.7490314245224,
"document": {
"title": "Adventures of Huckleberry Finn",
"author": "Mark Twain",
"edition": {
"format": "hardback",
"price": 18
}
,
"synopsis": "The adventures of Huck, a boy of 13, and Jim, an escaped slave, rafting down the Mississippi River in pre-Civil War America."
}
}
, ...
但是,我想知道如何调整查询,以便它只为每个找到匹配项的文档提供匹配的元素:
{
"results": [
{
"index": 0,
"uri": "/books/twain4.json",
"score": 14336,
"confidence": 0.432453483343124,
"fitness": 0.7490314245224,
"document": {
"author": "Mark Twain",
}
}
, ...
此处只有author元素匹配,但在执行查询时不知道此元素。我只想显示上面作者的匹配值。 提前谢谢!
答案 0 :(得分:3)
你有几个选择。
如果您只想针对一个JSON属性使用jSearch运行搜索,则可以运行以下命令:
const jsearch = require('/MarkLogic/jsearch');
const term = 'mark';
const query = cts.jsonPropertyWordQuery('author', term);
jsearch.documentSelect(cts.search(query), { snippet: { query: query } });
如果您想控制jSearch返回的内容,可以申请extraction:
const jsearch = require('/MarkLogic/jsearch');
const term = 'mark';
jsearch.documents().where(cts.wordQuery(term))
.map({ extract: { paths: ['/author'] }})
.result();
你也可以将两者结合起来:
const jsearch = require('/MarkLogic/jsearch');
const term = 'mark';
const query = cts.jsonPropertyWordQuery('author', term);
jsearch.documentSelect(cts.search(query), { snippet: { query: query }, extract: { paths: ['/author'] } });
有关详细信息,请阅读以下文档:http://docs.marklogic.com/guide/search-dev/javascript#id_71243
(还有一点需要注意:您可能还想对运行Query By Example感兴趣 - 您可以在其中构建原型文档并根据该文档执行搜索。更多信息请访问:http://docs.marklogic.com/guide/search-dev/javascript#id_60216