2

我一直在研究一个 Grails 项目,与 ElasticSearch ( v 20.6 ) 结合,使用自定义构建的 elasticsearch-grails-plugin (以支持 geo_point 索引:v.20.6) 一直在尝试进行过滤搜索,同时使用 script_fields (计算距离)。以下是闭包和从 GXContentBuilder 生成的 JSON:

关闭

records = Domain.search(searchType:'dfs_query_and_fetch'){
            query {                
                filtered = {
                    query = {
                        if(queryTxt){
                            query_string(query: queryTxt)
                        }else{
                            match_all {}
                        }
                    }
                    filter = {
                        geo_distance = {
                            distance = "${userDistance}km"
                        "location"{
                                lat = latlon[0]?:0.00
                                lon = latlon[1]?:0.00
                            }
                        }
                    }
                }
            }
            script_fields = {
                distance = {
                    script = "doc['location'].arcDistanceInKm($latlon)"
                }
            }
            fields = ["_source"]
        }

GXContentBuilder 生成的查询 JSON :

{
"query": {
    "filtered": {
        "query": {
            "match_all": {}
        },
        "filter": {
            "geo_distance": {
                "distance": "5km",
                "location": {
                    "lat": "37.752258",
                    "lon": "-121.949886"
                }
            }
        }
    }
},
"script_fields": {
    "distance": {
        "script": "doc['location'].arcDistanceInKm(37.752258, -121.949886)"
    }
},
"fields": ["_source"]
}

使用 curl-way 的 JSON 查询工作得非常好。但是当我尝试从 Groovy 代码中执行它时,我的意思是这个(取自 ElasticSearchService.groovy),其中请求是 SearchRequest 实例:

 elasticSearchHelper.withElasticSearch { Client client ->
        def response = client.search(request).actionGet()
 }

它抛出以下错误:

Failed to execute phase [dfs], total failure; shardFailures {[1][index][3]: SearchParseException[[index][3]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query_binary":"eyJxdWVyeSI6eyJmaWx0ZXJlZCI6eyJxdWVyeSI6eyJtYXRjaF9hbGwiOnt9fSwiZmlsdGVyIjp7Imdlb19kaXN0YW5jZSI6eyJkaXN0YW5jZSI6IjVrbSIsImNvbXBhbnkuYWRkcmVzcy5sb2NhdGlvbiI6eyJsYXQiOiIzNy43NTIyNTgiLCJsb24iOiItMTIxLjk0OTg4NiJ9fX19fSwic2NyaXB0X2ZpZWxkcyI6eyJkaXN0YW5jZSI6eyJzY3JpcHQiOiJkb2NbJ2NvbXBhbnkuYWRkcmVzcy5sb2NhdGlvbiddLmFyY0Rpc3RhbmNlSW5LbSgzNy43NTIyNTgsIC0xMjEuOTQ5ODg2KSJ9fSwiZmllbGRzIjpbIl9zb3VyY2UiXX0=","explain":true}]]]; nested: QueryParsingException[[index] No query registered for [query]]; }

如果我只使用上述闭包filtered = { ... } script_fields = { ... }但它不返回计算出的距离,则上述闭包有效。

有人有类似的问题吗?

提前致谢 :)

有可能我在这里指出了明显的问题:P

4

0 回答 0