API Demo
查数量
//count:数量
{
var countResponse1 = client.Count<ElasticUserModel>(c => c.Index("index13"));
var countResponse2 = client.Count<ElasticUserModel>(c => c.Index("index13").Query(q => q.Term("Age", "0")));
}
查询包含指定字段的文档
//exists:查询包含name字段的文档
{
var searchResponse1 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Exists(e => e.Field("name"))));
}
根据id查询
//get:根据id查询
{
var getResponse = client.Get<ElasticUserModel>(2, g => g.Index("index13"));
if (getResponse.IsValid)
{
var userModel = getResponse.Source;
}
}
match
var searchResponse1 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Match(m => m.Field("Description").Query("description 5"))));//默认or
var searchResponse2 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Match(m => m.Field("Description").Query("description 5").Operator(Operator.And))));
var searchResponse3 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Match(m => m.Field("Description").Query("description 5").Operator(Operator.Or))));
multiMatch
var searchResponse4 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.MultiMatch(m => m.Fields(new string[] { "Name", "Description" }).Query("description 3 5"))));
var searchResponse5 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.MultiMatch(m => m.Fields(new string[] { "Name", "Description" }).Query("description 5").Operator(Operator.And))));
var searchResponse6 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.MultiMatch(m => m.Fields(new string[] { "Name", "Description" }).Query("description 0 5").Type(TextQueryType.Phrase))));//0
var searchResponse7 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.MultiMatch(m => m.Fields(new string[] { "Name", "Description" }).Query("description 5").Type(TextQueryType.Phrase))));
var searchResponse8 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.MultiMatch(m => m.Fields(new string[] { "Name", "Description" }).Query("description 5").Operator(Operator.And))));
term
var searchResponse9 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Term(t => t.Field("Description").Value("5"))));
terms
var searchResponse10 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Terms(ts => ts.Field("Age").Terms(new int[] { 23, 24 }))));
Wildcard模糊查询
var searchResponse11 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Wildcard(w => w.Field("Description").Value("des*"))));
nested
var searchResponse12 = client.Search<ElasticUserModel>(s => s.Index("index13").Query(q => q.Nested(n => n.Path("Friends").Query(q => q.Term(t => t.Field("Friends.UserID").Value(1)) && q.Match(m => m.Field(f => f.Friends[0].Name).Query("fan1"))))));
bool
//且(&& Mush)
var searchResponse = client.Search<People>(s =>
s.Query(q =>
q.Bool(b =>
b.Must(
m => m.Term(t => t.Age, 4),
m => m.Term(t => t.Description.Suffix("keyword"), "jk lm")
)
)
)
);
//第二种写法,同上
searchResponse = client.Search<People>(s =>
s.Query(q =>
q.Term(p => p.Age, 4) && q.Term(p => p.Description.Suffix("keyword"), "jk lm")
)
);
//或(|| Should)
searchResponse = client.Search<People>(s =>
s.Query(q =>
q.Term(p => p.Age, 4) || q.Term(p => p.Description.Suffix("keyword"), "jk lm")
)
);
//非(! must_not)
searchResponse = client.Search<People>(s =>
s.Query(q =>
!q.Term(p => p.Age, 4) && !q.Term(p => p.Age, 2)
)
);
//过滤(+ filter)通过使用一元“+”运算符可以将查询转换为带有过滤子句的布尔查询。
searchResponse = client.Search<People>(s =>
s.Query(q =>
+q.Term(p => p.Age, 4)
)
);
//假设你有很多 must、should 子句,使用“&=”、“|=”进行合并
QueryContainer c = new QueryContainer();
for (var i = 1; i < 5; i++)
{
c |= new TermQuery { Field = "age", Value = i };
}
searchResponse = client.Search<People>(s =>
s.Query(q => c)
);
SearchRequest
var searchRequest = new SearchRequest
{
From = 0,
Size = 3,
Query = new TermQuery
{
Field = Infer.Field<People2>(p => p.Age),
Value = 2
}
};
var searchResponse20 = client.Search<People2>(searchRequest);
条件拼接
var response = GetResponse(1, 10, 0, "test1", "");
ISearchResponse<ElasticSpuModel> GetResponse(int pageIndex, int pageSize, int spuid, string title, string description)
{
var settings = new ConnectionSettings(new Uri("http://127.0.0.1:9200/")).DefaultFieldNameInferrer((name) => name).DefaultIndex("meshopindexname2");//meshopindexnamealias
settings.EnableDebugMode((apiCallDetails) =>
{
//请求完成 返回 apiCallDetails
var url = apiCallDetails.Uri.ToString();
var method = apiCallDetails.HttpMethod.ToString();
var code = apiCallDetails.HttpStatusCode;
var requestStr = System.Text.Encoding.Default.GetString(apiCallDetails.RequestBodyInBytes);
var responseStr = System.Text.Encoding.Default.GetString(apiCallDetails.ResponseBodyInBytes);
});
ElasticClient client = new ElasticClient(settings);
QueryContainerDescriptor<ElasticSpuModel> queryContainerDescriptor = new QueryContainerDescriptor<ElasticSpuModel>();
List<QueryContainer> mustQueryContainerList = new List<QueryContainer>();
//List<QueryContainer> shouldQueryContainerList = new List<QueryContainer>();
List<QueryContainer> mustNotQueryContainerList = new List<QueryContainer>();
IBoolQuery boolQuery = new BoolQuery();
//(title || handle) && spuid && description && state!=-1
if (spuid != 0)
{
mustQueryContainerList.Add(queryContainerDescriptor.Term(m => m.Field(p => p.SPUID).Value(spuid.ToString())));
}
if (!string.IsNullOrWhiteSpace(title))
{//title || handle
QueryContainer titleQueryContainer = queryContainerDescriptor.Match(d => d.Field(p => p.Title).Query(title))
|| queryContainerDescriptor.Match(d => d.Field(p => p.Handle).Query(title));
mustQueryContainerList.Add(titleQueryContainer);
}
if (!string.IsNullOrWhiteSpace(description))
{
mustQueryContainerList.Add(queryContainerDescriptor.Match(m => m.Field(f => f.Description).Query(description)));
}
//排除已删除产品
mustNotQueryContainerList.Add(queryContainerDescriptor.Term(t => t.Field(f => f.State).Value(-1)));
boolQuery.Must = mustQueryContainerList;
//boolQuery.Should = shouldQueryContainerList;
boolQuery.MustNot = mustNotQueryContainerList;
Func<QueryContainerDescriptor<ElasticSpuModel>, QueryContainer> query = cd => queryContainerDescriptor.Bool(d => boolQuery);
Func<SourceFilterDescriptor<ElasticSpuModel>, ISourceFilter> sourceSelector = product =>
product.Includes(product =>
product.Fields
(
product => product.SPUID,
product => product.Title,
product => product.Handle,
product => product.UpdateTime,
product => product.CreateTime
)
);
Func<SortDescriptor<ElasticSpuModel>, IPromise<IList<ISort>>> sortSelector = product => product.Field(f => f.UpdateTime, SortOrder.Descending).Field(f => f.CreateTime, SortOrder.Descending);
int from = (pageIndex - 1) * pageSize;
Func<SearchDescriptor<ElasticSpuModel>, ISearchRequest> querySelector = d => d.Query(query).Source(sourceSelector).Sort(sortSelector).From(from).Size(pageSize).Version();
var response = client.Search<ElasticSpuModel>(querySelector);
//var requestStr = System.Text.Encoding.Default.GetString(response.ApiCall.RequestBodyInBytes);
//var responseStr = System.Text.Encoding.Default.GetString(response.ApiCall.ResponseBodyInBytes);
return response;
}
source返回部分字段
//不返回_source
var searchResponse = client.Search<People>(s => s.Source(false).Query(q => q.MatchAll()));
//只返回id、age
searchResponse = client.Search<People>(s => s.Source(sf => sf.Includes(i => i.Fields(f => f.Id, f => f.Age))).Query(q => q.MatchAll()));
//不返回Name
searchResponse = client.Search<People>(s => s.Source(sf => sf.Excludes(i => i.Fields("name*"))).Query(q => q.MatchAll()));