Elasticsearch上下文建议,bool on contexts

时间:2015-04-24 08:35:00

标签: elasticsearch

我正在使用上下文建议器,我想知道我们是否可以设置上下文的范围用于建议而不是使用所有上下文。

目前查询需要匹配所有上下文。我们可以在上下文中添加“OR”操作和/或指定用于特定查询的上下文吗?

here为例: 映射:

PUT /venues/poi/_mapping
{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category"
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

然后我索引一份文件:

 {
  "suggest_field": {
    "input": ["The Shed", "shed"],
    "output" : "The Shed - fresh sea food",
    "context": {
      "location": {
        "lat": 51.9481442,
        "lon": -5.1817516
      },      
      "type" : "restaurant"
    }
  }
}

查询:

{
  "suggest" : {
    "text" : "s",
    "completion" : {
      "field" : "suggest_field",
      "context": {
        "location": {
          "value": {
            "lat": 51.938119,
            "lon": -5.174051
          }
        }
      }
    }
  }
}

如果我只使用一个Context(上例中的“location”)查询它会给出错误,我需要传递两个上下文,是否可以指定使用哪个上下文?或者将“Context_Operation”参数设置为“OR”。

1 个答案:

答案 0 :(得分:2)

您有两个选择:

首先,在映射中添加所有可用的类型值作为默认值(不可伸缩)

{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category",
            "default": ["restaurant", "pool", "..."]
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

第二个选项,您为每个索引文档添加默认值,并且仅将此值添加为默认值

映射:

{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category",
            "default": "any"
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

文件:

{
  "suggest_field": {
    "input": ["The Shed", "shed"],
    "output" : "The Shed - fresh sea food",
    "context": {
      "location": {
        "lat": 51.9481442,
        "lon": -5.1817516
      },      
      "type" : ["any", "restaurant"]
    }
  }
}