ElasticSearch是否有任何好的布尔查询解析器?

时间:2019-05-09 09:24:31

标签: elasticsearch

ElasticSearch或其他开放源代码中是否有任何可将布尔查询转换为ElasticSearch查询的库?

使用典型的布尔查询表达式(AND,OR,“,”,* 、?)转换为ElasticSearch的“ json”查询,并创建“ musts”,“ shoulds”等...

例如,我的意思是将其转换:

(city = 'New York' AND state = 'NY') AND ((businessName='Java' and businessName='Shop') OR (category='Java' and category = 'Shop'))

对此:

{
    "query": {
        "match_all": {}
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "city": "New york"
                    }
                },
                {
                    "term": {
                        "state": "NY"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "businessName": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "businessName": "Shop"
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "category": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "category": "Shop"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

1 个答案:

答案 0 :(得分:1)

有一个名为luqum的Python库可以完全满足您的需求。

该库会将Lucene表达式解析为抽象语法树。然后,您可以使用该树和generate the Elasticsearch JSON DSL equivalent query