SuggestCompletion Nest用法

时间:2014-05-18 00:16:46

标签: c# elasticsearch nest

我尝试对某个位置(国家/地区和城市)进行SuggestCompletion查询,我想对这两个字段执行查询。

到目前为止,我的映射如下:

var response =  _client.CreateIndex(PlatformConfiguration.LocationIndexName,
                    descriptor => descriptor.AddMapping<LocationInfo>(
                        m => m.Properties(
                            p => p.Completion(s => s
                                .Name(n=>n.CountryName)
                                .IndexAnalyzer("simple")
                                .SearchAnalyzer("simple")
                                .MaxInputLength(50)
                                .Payloads()
                                .PreserveSeparators()
                                .PreservePositionIncrements()).
                                Completion(s=>s.Name(n => n.City)
                                .IndexAnalyzer("simple")
                                .SearchAnalyzer("simple")
                                .MaxInputLength(50)
                                .Payloads()
                                .PreserveSeparators()
                                .PreservePositionIncrements())
                            )));

修改 我如何索引元素:

public bool IndexLocations(IList<LocationInfo> locations)

        {
            var bulkParams = locations.Select(p => new BulkParameters<LocationInfo>(p){
                Id = p.Id, 
                Timestamp = DateTime.Now.ToTimeStamp()
            });
            var response = _client.IndexMany(bulkParams, PlatformConfiguration.LocationIndexName);
            return response.IsValid;
        }

修改

查看映射后,我将查询更改为以下内容:

var response = _client.Search<LocationInfo>(location =>
                location.Index(PlatformConfiguration.LocationIndexName).
                    SuggestCompletion("locationinfo", f => f.OnField("countryName").Text(text).Size(1)));

我也尝试过:

 var response = _client.Search<LocationInfo>(location =>
                location.Index(PlatformConfiguration.LocationIndexName).
                    SuggestCompletion("countryName", f => f.OnField("countryName").Text(text).Size(1)));  

.....我仍然得到一个空的结果

映射

{
  "locationindex": {
    "mappings": {
      "locationinfo": {
        "properties": {
          "countryName": {
            "type": "completion",
            "analyzer": "simple",
            "payloads": true,
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
          }
        }
      },
      "bulkparameters`1": {
        "properties": {
          "document": {
            "properties": {
              "city": {
                "type": "string"
              },
              "countryName": {
                "type": "string"
              },
              "countryTwoDigitCode": {
                "type": "string"
              },
              "id": {
                "type": "string"
              },
              "latitude": {
                "type": "string"
              },
              "longitude": {
                "type": "string"
              }
            }
          },
          "id": {
            "type": "string"
          },
          "timestamp": {
            "type": "long"
          },
          "versionType": {
            "type": "long"
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

IndexMany()

删除了BulkParameters NEST 1.0.0 beta 1Bulk()的支持

如果要使用具有更高级参数的批量,则现在必须使用BulkParameters命令。

该版本仍然附带"bulkparameters``1``"in the assembly

此后已在开发分支中删除。

所以现在发生的是你实际上是在索引"locationinfo"类型文档而不是"locationinfo"。因此,为Bulk()指定的映射不起作用。

在为各个项目配置高级参数时,请参阅here for an example,了解如何使用{{1}}一次索引多个对象。