本文介绍了MarkLogic 9 cts.parse无法正确解析查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MarkLogic 9开发基于Web的搜索应用程序.我有一个查询构建界面,该界面允许用户在与db中文档的特定JSON属性相对应的文本框中输入字符串.想法是用户可以完全按照cts.parse(我使用服务器端javascript,而不是XQuery)期望的方式输入搜索词,这样他们的搜索就可以任意复杂,而我不必自己解析查询.但是,在进行了一些测试之后,我发现了在布尔逻辑中使用括号的奇怪现象.也就是说,当您在圆括号中添加诸如cat和(dog OR bird)之类的语句时,cts.parse会将OR误认为搜索项.

I am working on a web-based search application using MarkLogic 9. I have a query building interface that allows users to enter strings into textboxes that correspond to particular JSON properties of the documents in the db. The idea was that the user could enter the search terms exactly as the cts.parse (I use server side javascript, not XQuery) expects them, so that their searches could be arbitrarily complex and I would not have to deal with parsing the queries myself. However after doing some testing, I have discovered an odd phenomena regarding the use of parentheses in the Boolean logic. Namely, when you include parentheses with a statement like cat and (dog OR bird), cts.parse will mistake the OR for a search term.

我将通过我的网站提供一个实际示例:

I will provide an actual example from my website:

我已经构造了一个绑定对象,将查询绑定到文档的元素上,

I have constructed a bindings object to bind the queries to the elements of my documents,

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),
					])
		);
	},	
}

例如,我的服务器端脚本调用

My server-side scripts call, for example,

cts.parse('main:'+params.mainQuery,bind)

以下是输入的字符串和返回的查询的一些示例:

Here are some examples of the strings entered and the queries returned:

  1. 大脑或心脏或肺部
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,但对于其他两个术语却无法生成jsonPropertyWordQuery,仅为其生成cts.wordQuery()

This one properly generates the jsonPropertyWordQuery for the 3 fields (title,abstract, mesh terms) for the "brain" term, but fails to do so for the other two terms, for which it simply generates a cts.wordQuery().

  1. 大脑或心脏与肺
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"])], [])
  1. 脑部OR(心脏和肺部)
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,但其他术语与基本单词查询一样,我试图避免这种情况.

2 and 3 appear to be the same. The first part correct generates a jsonPropertyWordQuery but the other terms are anded as basic word queries, which I am trying to avoid.

  1. (大脑或心脏)和肺部
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作为搜索项包括在内.

Here, the parser does not seem to recognize that OR is an operator because, even though it is correctly generating jsonPropertyWordQueries, it is including OR as a term in the search.

老实说,我在查找任何正确的查询时遇到了麻烦,这使我相信我一定做错了什么.我不知道那可能在哪里.我在滥用cts.parse或绑定对象吗?

Honestly, I am having trouble finding any query that is correctly, which leads me to believe that I must be doing something wrong. I have no idea where that could be. Am I misusing cts.parse or the bindings object?

任何帮助将不胜感激.

Any help would be greatly appreciated.

推荐答案

我不清楚您确切的查询字符串是什么.

It isn't clear to me what you exact query string is.

如果查询字符串类似于"main:(cat OR dog)",则OR在该上下文中不是关键字.标记非常有限之后才允许的内容,而不是查询语言的全部范围,它只是文字列表.

If the query string is something like "main:(cat OR dog)" then the OR is not a keyword in that context. What is allowed after a tag is pretty limited, and is not the full range of query language, it it just a list of literals.

如果查询字符串类似于"main:cat OR dog,则标记的范围仅为cat.

If the query string is something like "main:cat OR dog then the scope of the tag is just cat.

让标记后的()限定整个查询范围并不是没有道理的,现在您可以将函数绑定到标记(将其固定到范围索引或字段时没有任何意义),但这不是语法是怎么建立的.

It isn't unreasonable to want the () after a tag to scope an entire query now you can bind a function to the tag (it made no sense when it was fixed to a range index or field), but that isn't how the grammar is set up.

所以您只需要零碎地做一些事情:main:cat OR main:dog

So you'll just have to do things piecemeal: main:cat OR main:dog

或:给定传递给函数的值集,对它们进行空间级联,然后将其传递给对cts:parse的单独调用,以将其解释为可以包装的查询.

Or: given the set of values passed into your function, space-concatenate them and pass that to a separate call to cts:parse to get them interpreted as a query you can wrap.

这篇关于MarkLogic 9 cts.parse无法正确解析查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:06