SuggestCompletion Nest usage

2019-08-19 09:19发布

问题:

I'm trying to do a SuggestCompletion query for a location (countries and cities), I'd like to perform the query over those two fields.

my mapping so far is the following:

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())
                            )));

Edit: How I'm indexing the elements:

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;
        }

Edit

After viewing the mappings I changed my query to the following:

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

and I also I tried:

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

.....And I still get an empty result

the mapping

{
  "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:

The support for IndexMany() with wrapped BulkParameters has been removed in NEST 1.0.0 beta 1

If you want to use a bulk with more advanced parameters you now have to use the Bulk() command.

The beta sadly still shipped with the BulkParameters class in the assembly

This has since been removed in the develop branch.

So what happens now is that you are actually indexing "bulkparameters``1``" type documents and not "locationinfo". So the mapping specified for "locationinfo" does not come into play.

See here for an example on how to use Bulk() to index many objects at once while configuring advanced parameters for individual items.