一、创建索引和Mapping(注意一定要指定分词器 analyzer,要不然第三步的查询,就查询不正确(但使用ElasticJsonClient可以),在这个问题上折腾了很久很久直到怀疑人生)
PUT person_index { "mappings": { "personn": { "properties": { "id":{ "type":"integer" }, "name":{ "type":"text", "analyzer":"standard" }, "address": { "type": "text", "analyzer":"standard" } } } } }
二、生成索引下的数据
PUT person_index/personn/1 { "id":1, "name":"张三", "address":"北京海淀区352号" } PUT person_index/personn/2 { "id":2, "name":"李四", "address":"天津市滨海新区88号" } PUT person_index/personn/3 { "id":3, "name":"王五张", "address":"无" }
var person = new Person { Id = 1, FirstName = "china", LastName = "Laarman" }; var indexResponse = client.Index(person, c => c.Index("people").Type("person"));
三、查询代码
string indexName = "person_index"; var searchResponse = eSSever.ElasticLinqClient.Search<Personn>(s => s .From(0) .Size(10).Index(indexName) .Query(q => q .Match(m => m .Field(f => f.Name) .Query("张") ) ) ); var peoples = searchResponse.Documents;
四、也可以使用lowLevel的形式传值
var searchResponse = client.LowLevel.Search<SearchResponse<Person>>("people", PostData.Serializable(new { from = 0, size = 10, query = new { multi_match = new { fields = new string[] { "firstName", "lastName" }, query = "Laarman" } } })); var responseJson = searchResponse; var docs = responseJson.Documents;