我正在使用 MarkLogic 9 开发基于 Web 的搜索应用程序。我有一个查询构建界面,允许用户将字符串输入到与数据库中文档的特定 JSON 属性相对应的文本框中。这个想法是用户可以完全按照 cts.parse(我使用服务器端 javascript,而不是 XQuery)所期望的那样输入搜索词,这样他们的搜索就可以任意复杂,我不必自己处理解析查询. 然而,在做了一些测试之后,我发现了一个关于在布尔逻辑中使用括号的奇怪现象。也就是说,当您在 cat 和 (dog OR bird) 之类的语句中包含括号时,cts.parse 会将 OR 误认为是搜索词。
我将从我的网站提供一个实际示例:
我已经构建了一个绑定对象来将查询绑定到我的文档的元素,
var qOpts = ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded"];
var bindings = {
main: function(operator, values, options){
return(
cts.orQuery([
cts.jsonPropertyWordQuery('title',values,qOpts),
cts.jsonPropertyWordQuery('abstract',values,qOpts),
cts.jsonPropertyWordQuery('meshterms',values,qOpts),
])
);
},
}
例如,我的服务器端脚本调用
cts.parse('main:'+params.mainQuery,bind)
以下是输入的字符串和返回的查询的一些示例:
- 脑或心或肺
cts.orQuery([cts.jsonPropertyWordQuery("title", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("abstract", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("meshterms", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.wordQuery("heart", ["lang=en"], 1), cts.wordQuery("lung", ["lang=en"], 1)], [])
这个正确地为“大脑”术语的 3 个字段(标题、摘要、网格术语)生成 jsonPropertyWordQuery,但对于其他两个术语没有这样做,它只是生成一个 cts.wordQuery()。
- 大脑或心脏和肺
cts.orQuery([cts.jsonPropertyWordQuery("title", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("abstract", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("meshterms", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.andQuery([cts.wordQuery("heart", ["lang=en"], 1), cts.wordQuery("lung", ["lang=en"], 1)], ["unordered"])], [])
- 脑或(心脏和肺)
cts.orQuery([cts.jsonPropertyWordQuery("title", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("abstract", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("meshterms", "brain", ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.andQuery([cts.wordQuery("heart", ["lang=en"], 1), cts.wordQuery("lung", ["lang=en"], 1)], ["unordered"])], [])
2和3看起来是一样的。第一部分正确生成一个 jsonPropertyWordQuery,但其他术语作为基本单词查询,我试图避免。
- (大脑或心脏)和肺
cts.andQuery([cts.orQuery([cts.jsonPropertyWordQuery("title", ["brain", "OR", "heart"], ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("abstract", ["brain", "OR", "heart"], ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1), cts.jsonPropertyWordQuery("meshterms", ["brain", "OR", "heart"], ["case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"], 1)], []), cts.wordQuery("lung", ["lang=en"], 1)
在这里,解析器似乎没有识别出 OR 是一个运算符,因为即使它正确生成了 jsonPropertyWordQueries,它也在搜索中包含了 OR 作为一个术语。
老实说,我很难找到任何正确的查询,这让我相信我一定做错了什么。我不知道那可能在哪里。我是否在滥用 cts.parse 或绑定对象?
任何帮助将不胜感激。