到目前为止,我有这个(用于编制索引):
var createIndexResponse = await elasticClient.CreateIndexAsync(indexName, c => c
.InitializeUsing(indexConfig)
.Mappings(m => m
.Map<ElasticsearchModel>(mm => mm
.Properties(
p => p
.Completion(cp => cp
.Name(elasticsearchModel => elasticsearchModel.StringTest)
.Analyzer("simple")
.SearchAnalyzer("simple")
)
.Text(t => t.Name(elasticsearchModel => elasticsearchModel.StringTest).Analyzer("customAnalyzerLowercaseSynonymAsciifolding"))
)
)
)
);
我在这篇文章的帮助下得到了这个(但我不确定这是否正确,因为我认为我缺少一些属性来设置min。建议长度等):https://stackoverflow.com/a/33796953/7199922 < / p>
现在我无法弄清楚如何查询它以获得建议结果。
我搜索了像search suggest site:https://github.com/elastic/elasticsearch-net/
这样的git repo并检查了文档,但找不到多少。
答案 0 :(得分:0)
我最终得到了以下内容:
<强>模型强>
public class ElasticsearchModel
{
public int IntTest { get; set; }
public string StringTest { get; set; }
public CompletionField StringTestSuggest
{
get
{
return new CompletionField
{
Input = new[] { StringTest + " ThisIsTheStringTestSuggestField" }
};
}
}
}
<强>索引强>
var indexSettings = new IndexSettings
{
NumberOfReplicas = 0, // If this is set to 1 or more, then the index becomes yellow.
NumberOfShards = 5
};
var indexConfig = new IndexState
{
Settings = indexSettings
};
var createIndexResponse = elasticClient.CreateIndex(indexName, c => c
.InitializeUsing(indexConfig)
.Mappings(m => m
.Map<ElasticsearchModel>(mm => mm
.Properties(
p => p
.Completion(cp => cp
.Name(elasticsearchModel => elasticsearchModel.StringTestSuggest)
.Analyzer("customAnalyzerLowercaseSynonymAsciifolding")
.SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding")
)
.Text(t => t.Name(product => product.IntTest).Analyzer("keyword"))
.Text(t => t.Name(elasticsearchModel => elasticsearchModel.StringTest).Analyzer("customAnalyzerLowercaseSynonymAsciifolding")) // You can use "standard" here or some other default analyzer.
)
)
)
);
elasticClient.Refresh(indexName);
<强>查询强>
public List<ElasticsearchModel> Suggest(string indexName, string query)
{
var elasticClient = ElasticsearchHelper.DatabaseConnection();
var response = elasticClient.Search<ElasticsearchModel>(s => s
.Index(indexName)
.Suggest(su => su
.Completion("StringTest", cs => cs
.Field(f => f.StringTestSuggest)
.Prefix(query)
.Fuzzy(f => f
.Fuzziness(Fuzziness.Auto)
)
.Size(5)
)
)
);
var suggestions =
from suggest in response.Suggest["StringTest"]
from option in suggest.Options
select new ElasticsearchModel
{
IntTest = option.Source.IntTest,
StringTest = option.Source.StringTest
};
return suggestions.ToList();
}
其他事项(更容易理解方法)
public void Refresh(string indexName)
{
var elasticClient = ElasticsearchHelper.DatabaseConnection();
elasticClient.Refresh(indexName); // This should be called before every query or everytime documents get indexed! Else some results might be missing.
}
启动与ES DB的连接
public static class ElasticsearchHelper
{
public static ElasticClient DatabaseConnection()
{
var node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
//var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node)
.PrettyJson()
.DisableDirectStreaming() // Enable for API HTTP request/response debugging.
.OnRequestCompleted(callDetails =>
{
// log out the request
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("REQUEST:");
Console.ResetColor();
if (callDetails.RequestBodyInBytes != null)
{
Console.WriteLine(
$"{callDetails.HttpMethod} {callDetails.Uri} \n" +
$"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
}
else
{
Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
}
// log out the response
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("RESPONSE:");
Console.ResetColor();
if (callDetails.ResponseBodyInBytes != null)
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
$"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
$"{new string('-', 30)}\n");
}
else
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
$"{new string('-', 30)}\n");
}
}) // For debugging...
; // Index name must be lowercase, cannot begin with an underscore, and cannot contain commas.
var client = new ElasticClient(settings);
return client;
}
}
所有信用和感谢帮助Russ Cam
。