【发布时间】:2019-01-06 04:13:49
【问题描述】:
如何将中缀表达式转换为弹性搜索查询
我的运营商是! + *
用户可以使用这些运算符进行任何表达式,例如:(((A*B*(!C))*(D*E))+F)*G
我希望在弹性搜索中将其转换为bool query
编辑
我不知道为什么我之前不这么说,但是我已经编写了一个将中缀转换为后缀表达式的代码,然后我调用了一个非常脏的递归方法来创建should (+), must (*) and must_not (!),但我正在寻找的是一种优化的方式为我做这个把戏。
我最后的查询是这样的,我认为这是非常非常讨厌的:
{
"from": 0,
"size": 10,
"_source": [
"*"
],
"index": "insta_userpost_new2",
"body": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "G"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"caption.text": "F"
}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "E"
}
},
{
"match_phrase": {
"caption.text": "D"
}
}
]
}
},
{
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"match_phrase": {
"caption.text": "C"
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"caption.text": "B"
}
},
{
"match_phrase": {
"caption.text": "A"
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
}
【问题讨论】:
-
*表示 AND 和+表示 OR 吗?还是这些符号有其他含义? -
@Val 是的,它们的意思是 OR 和 AND
-
好的,谢谢,今天晚些时候我会提供答案
标签: php algorithm elasticsearch