我希望允许我们的用户通过文本和地理位置搜索对象。不过,当按文本搜索时,我希望它能像使用普通查询和完成提示一样向建议术语。当我试图将suggestor添加到我的过滤查询中时,我得到了一个错误。有什么建议吗?
我的地图:
curl -XPUT http://localhost:9200/objects/object/_mapping -d '{
"object": {
"properties": {
"objectDescription": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "standard",
"search_analyzer": "standard",
"payloads": true
}
}
}
}'
curl -X PUT http://localhost:9200/objects/object/_mapping -d '{
"object":{"properties":{"location":{"type":"geo_point"}}}
}'
我的搜索:
{
"query": {
"filtered": {
"query": {
"fuzzy": {
"objectDescription": {
"value": "burger"
}
}
},
"filter": {
"or": [
{
"geo_distance": {
"distance": "15mi",
"location": {
"lat": 33.4255104,
"lon": -111.94000540000002
}
}
},
{
"bool": {
"must": [
{
"term": {
"administrative_area_level_1": "AZ"
}
},
{
"term": {
"addressType": "administrative_area_level_1"
}
}
]
}
}
]
}
}
}
}
当我在没有过滤器的情况下添加与查询相同级别的建议时,它会忽略我的过滤器。当搜索时,它会找到不在纬度附近的物体。
{
"query":{
"filtered":{
"query":{
"fuzzy":{
"objectDescription":{"value":"bears"}
}
},
"filter":{
"bool":{
"should":[
{
"geo_distance":{
"distance":"15mi",
"location":{
"lat":43.6187102,
"lon":-116.21460680000001
}
}
},
{
"bool":{
"must":[
{
"term":{"administrative_area_level_1":"ID"}
},{
"term":{"addressType":"administrative_area_level_1"}
}
]
}
}
]
}
}
}
},
"suggest":{
"object-suggest":{
"text":"bears",
"completion":{
"field":"suggest",
"fuzzy":{"fuzziness":2}
}
}
}
}
当我把正文的建议部分放在查询的位置时,我会得到这个错误。
{
"query":{
"filtered":{
"suggest":{
"object-suggest":{
"text":"bears",
"completion":{
"field":"suggest",
"fuzzy":{"fuzziness":2}
}
}
},
"filter":{
"bool":{
"should":[
{
"geo_distance":{
"distance":"15mi",
"location":{
"lat":43.6187102,
"lon":-116.21460680000001
}
}
},
{
"bool":{
"must":[
{
"term":{"administrative_area_level_1":"ID"}
},{
"term":{"addressType":"administrative_area_level_1"}
}
]
}
}
]
}
}
}
}
}
所以我的问题是,我在elastic.co或stackoverflow上还没有找到一个将地理位置搜索与完成建议相结合的例子。
这样试试。suggest
块单独位于顶层:
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"geo_distance": {
"distance": "15mi",
"location": {
"lat": 43.6187102,
"lon": -116.21460680000001
}
}
},
{
"bool": {
"must": [
{
"term": {
"administrative_area_level_1": "ID"
}
},
{
"term": {
"addressType": "administrative_area_level_1"
}
}
]
}
}
]
}
}
}
},
"suggest": {
"object-suggest": {
"text": "bears",
"completion": {
"field": "suggest",
"fuzzy": {
"fuzziness": 2
}
}
}
}
}
更新
对于您的精确用例,我认为您应该使用带有地理位置上下文的上下文提示,而不是简单的完成提示。这当然是你实现期望所需要的。