简体   繁体   English

将中缀表达式转换为弹性搜索查询

[英]Convert an infix expression to elastic search query

How can I convert an infix expression to an elastic search query如何将中缀表达式转换为弹性搜索查询
my operators are ! + *我的运营商是! + * ! + *
and user may make any expression using those operators, something like:用户可以使用这些运算符进行任何表达式,例如:
(((A*B*(!C))*(D*E))+F)*G
and I wish to convert it to a bool query in elastic search我希望在弹性搜索中将其转换为bool 查询

Edit编辑
I don't know why I didn't say this earlier but I have already written a code to convert infix to postfix expression and then I call a very dirty recursive method to create should (+), must (*) and must_not (!) but what i'm seeking is an optimized way to do the trick for me.我不知道为什么我不早说,但我已经写了一段代码将中缀转换为后缀表达式,然后我调用了一个非常脏的递归方法来创建should (+), must (*) and must_not (!)但我正在寻找一种优化的方式来为我做这个伎俩。

My query at the end is something like this which I think is very very nasty:我最后的查询是这样的,我认为这是非常非常讨厌的:

{
  "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"
                                          }
                                        }
                                      ]
                                    }
                                  }
                                ]
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

I would maybe try to leverage simply_query_string .我可能会尝试利用simply_query_string For that, you'd have to:为此,您必须:

  • replace + by |替换+| (for the OR ) (对于OR
  • then replace * by + (for the AND )然后用+替换* (对于AND
  • finally replace !终于换了! by - (for the NOT ) by - (对于NOT

So if a user inputs this:所以如果用户输入这个:

(((A*B*(!C))*(D*E))+F)*G

You'd end up with this你会得到这个

(((A+B+(-C))+(D+E))|F)+G

Which is a boolean expression that you can directly use in a simply_query_string query.这是一个布尔表达式,您可以直接在simply_query_string查询中使用。

GET /_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content"],
            "query" : "(((A+B+(-C))+(D+E))|F)+G"
        }
    }
}

You can use Elasticsearch scripts in queries, like this: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html您可以在查询中使用 Elasticsearch 脚本,如下所示: https : //www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html

There are few scripting options, with the most simple and strait forward is 'Painless' scripts.脚本选项很少,最简单和最严格的是“无痛”脚本。 From elastic documentation:从弹性文档:

When you define a scripted field in Kibana, you have a choice of scripting languages.在 Kibana 中定义脚本字段时,您可以选择脚本语言。 Starting with 5.0, the default options are Lucene expressions and Painless从 5.0 开始,默认选项是 Lucene 表达式和 Painless

Also you can return the result of the calculation using Scripted Fields: https://www.elastic.co/guide/en/kibana/current/scripted-fields.html您也可以使用脚本字段返回计算结果: https : //www.elastic.co/guide/en/kibana/current/scripted-fields.html

You can run an infix expression evaluation[1] and replace the standard eval operations with DSL bool query composers.您可以运行中缀表达式求值 [1] 并用 DSL bool 查询编写器替换标准的 eval 操作。

We actually do something akin to this for https://opensource.appbase.io/mirage/ (you can try it live), where we map GUI blocks to a composable bool query.我们实际上为https://opensource.appbase.io/mirage/做了一些类似的事情(你可以实时尝试),我们将 GUI 块映射到一个可组合的 bool 查询。 The source code is viewable at https://github.com/appbaseio/mirage .源代码可在https://github.com/appbaseio/mirage上查看。

[1] Ref: https://www.geeksforgeeks.org/expression-evaluation/ [1] 参考: https : //www.geeksforgeeks.org/expression-evaluation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM