Elasticsearch:生成的查询无效

时间:2014-11-09 23:59:57

标签: elasticsearch

在阅读完文档之后,我对弹性搜索感到有点沮丧,但似乎无法超越“生成的查询无效”响应。我想要做的是使用elasticsearch查找不完美的重复项在地理空间信息和相当大的数据集中。我想匹配名称(提升)和地址,过滤结果一个小的地理框,然后降低比我的参考点更远的匹配的相关性得分。有人可以帮忙吗?我我认为我已经了解了查询的各个元素,我的主要问题是将这些元素放在一起产生有效的东西。

$query = new \Elastica\Query\Builder('{
        "function_score": {
            "functions": [
                {
                    "gauss": {
                        "location": {
                            "origin": "'.$latitude.', '.$longitude.'",
                            "scale": "2km"
                        }
                    }
                }
            ],
            "query": {
                "filtered": {
                    "query": {
                        "bool": {
                            "should": [
                                {
                                    "match": {
                                        "name": {
                                            "query": "'.$name.'",
                                            "boost": 4
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "address": {
                                            "query": "'.$address.'",
                                            "boost": 1
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    "filter": {
                        "geo_distance": {
                            "distance": "2km",
                            "location": {
                                "lat": "'.$latitude.'",
                                "lon": "'.$longitude.'"
                            }
                        }
                    }
                }

            }
    }')

1 个答案:

答案 0 :(得分:0)

您应该通过"查询"来围绕整个查询。条款:

$query = new \Elastica\Query\Builder('{
"query": {
    "function_score": {
        "functions": [
            {
                "gauss": {
                    "location": {
                        "origin": "'.$latitude.', '.$longitude.'",
                        "scale": "2km"
                    }
                }
            }
        ],
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "name": {
                                        "query": "'.$name.'",
                                        "boost": 4
                                    }
                                }
                            },
                            {
                                "match": {
                                    "address": {
                                        "query": "'.$address.'",
                                        "boost": 1
                                    }
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "geo_distance": {
                        "distance": "2km",
                        "location": {
                            "lat": "'.$latitude.'",
                            "lon": "'.$longitude.'"
                        }
                    }
                }
            }
        }
    }
}')

要在查询时获得更多反馈,一个好习惯是打印Elasticsearch对查询的响应。

SENSE

中尝试查询
POST test/_search
{
        "function_score": {
            "functions": [
                {
                    "gauss": {
                        "location": {
                            "origin": "11, 12",
                            "scale": "2km"
                        }
                    }
                }
            ],
            "query": {
                "filtered": {
                    "query": {
                        "bool": {
                            "should": [
                                {
                                    "match": {
                                        "name": {
                                            "query": "name",
                                            "boost": 4
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "address": {
                                            "query": "address",
                                            "boost": 1
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    "filter": {
                        "geo_distance": {
                            "distance": "2km",
                            "location": {
                                "lat": "'.$latitude.'",
                                "lon": "'.$longitude.'"
                            }
                        }
                    }
                }
            }
        }
}

您将收到回复:

  

SearchParseException [[test] [4]:from [-1],size [-1]:Parse Failure [没有元素[function_score]的解析器]]; }]

告诉Elasticsearch没有认识到" function_score"。通过详细阅读elasticsearch wiki上的function_score页面并查看示例,您将找到所遗漏的内容:周围的"查询"子句。

PS:Elastica还提供了alternative syntax来使用ElasticaQueryBuilder创建查询体,这使您无需编写json。