ElasticsearchCRUD使用(十四)【ElasticsearchCRUD搜索查询和过滤】
来源:互联网 发布:廖雪峰python教程视频 编辑:程序博客网 时间:2024/06/05 03:01
本文演示如何使用ElasticsearchCRUD进行搜索。 API提供了可以与Elasticsearch的搜索API一起使用的搜索模型。 您可以搜索同步/异步,并提供您自己的JSON字符串或使用搜索模型。
开始:
ElasticsearchCRUD提供了一些数字或搜索的可能性。 这篇文章展示了不同的例子,这些例子应该有助于理解或使它更容易使用搜索模型参数。 当配置和创建模型时,可以使用以下方法作为HTTP请求发送:
public ResultDetails<SearchResult<T>> Search<T>( Search search, SearchUrlParameters searchUrlParameters = null)//or public async Task<ResultDetails<SearchResult<T>>> SearchAsync<T>( string searchJsonParameters, SearchUrlParameters searchUrlParameters = null)
搜索模型可以发送具有过滤器,查询或两者都有。 以下是一个简单的MatchAllFilter
的例子:
var search = new Search { Filter = new Filter( new MatchAllFilter() )};
搜索模型包含一个Filter
属性和一个Query
属性,它可以包含任何查询或任何过滤器类型。 可以根据需要选择所需的对象。
以下是一个简单的PrefixQuery
示例:
var search = new Search{ Query = new Query(new PrefixQuery("name", "on"))};
或者更复杂的搜索查询可能如下所示:
var search = new Search{ Query = new Query( new Filtered( new Filter( new MatchAllFilter { Boost = 1.1 } ) ) { Query = new Query(new MatchAllQuery()) } )};
以上搜索将发送到Elasticsearch如下:
POST http://localhost:9200/<index>/<type>/_search HTTP/1.1Content-Type: application/jsonHost: localhost:9200Content-Length: 86Expect: 100-continue{ "query":{ "filtered":{ "query":{"match_all":{}}, "filter":{"match_all":{"boost":1.1}} } }}
您还可以使用Search类的ToString()
方法来显示将发送到Elasticsearch的JSON查询。 调试查询或过滤器时,这非常有用。
GeoShapeQuery的一个例子:
var search = new Search{ Query = new Query( new GeoShapeQuery("circletest", new GeoShapePolygon { Coordinates = new List<List<GeoPoint>> { new List<GeoPoint> { new GeoPoint(40,40), new GeoPoint(50,40), new GeoPoint(50,50), new GeoPoint(40,50), new GeoPoint(40,40) } } } ) )};
被翻译成:
{ "query":{ "geo_shape":{ "circletest":{ "shape":{ "type":"polygon", "coordinates":[[ [40.0,40.0], [50.0,40.0], [50.0,50.0], [40.0,50.0], [40.0,40.0] ]] } } } }}
具有线性衰减功能的FunctionScoreQuery可以这样配置:
var search = new Search{ Query = new Query( new FunctionScoreQuery( new MatchAllQuery(), new List<BaseScoreFunction> { new LinearGeoPointFunction( "location", new GeoPoint(40,40), new DistanceUnitKilometer(100) ) { Decay=0.3, Filter = new MatchAllFilter(), Offset= 3, Weight= 3.0 } } ) )};
排序
搜索模型还支持排序,正常排序,GeoPoint排序或脚本排序。
以下是Geo排序示例:
var search = new Search{ Query = new Query(new MatchAllQuery()), Sort = new SortHolder( new List<ISort> { new SortGeoDistance("location", DistanceUnitEnum.km) { GeoPoints = new List<GeoPoint> { new GeoPoint(46, 46), new GeoPoint(49, 46), }, Order=OrderEnum.asc, Mode = SortModeGeo.max } } )};
或另一个脚本排序示例:
var search = new Search{ Query = new Query(new MatchAllQuery()), Sort = new SortScript("doc['lift'].value * factor") { Order = OrderEnum.asc, ScriptType= "number", Params = new List<ScriptParameter> { new ScriptParameter("factor", 1.5) } }};
在撰写本文时,应支持不同查询和过滤器的所有可能配置。 一些Elasticsearch文档不是实际的,一些记录的特征不起作用。 所有的搜索模型类都有使用真正的Elasticsearch 搜索引擎(elasticsearch-1.4.2)的集成测试。 这将有助于保持代码接近实际情况。
- ElasticsearchCRUD使用(十四)【ElasticsearchCRUD搜索查询和过滤】
- ElasticsearchCRUD使用(十七)【Elasticsearch搜索多个指标和类型】
- ElasticsearchCRUD使用(一)【介绍】
- ElasticsearchCRUD使用(十八)【进行MVC搜索Elasticsearch高亮】
- ElasticsearchCRUD使用(十六)【Elasticsearch聚合】
- ElasticsearchCRUD使用(十九)【索引热身】
- ElasticsearchCRUD使用(八)【使用Elasticsearch和WebAPI导出CSV】
- ElasticsearchCRUD使用(十)【Elasticsearch类型与ElasticsearchCRUD的映射】
- ElasticsearchCRUD使用(六)【EF和Elasticsearch的MVC应用程序】
- ElasticsearchCRUD使用(九)【Elasticsearch父子,孙子节点文件和路由】
- ElasticsearchCRUD使用(三)【嵌套文档的MVC】
- ElasticsearchCRUD使用(十一)【Elasticsearch同义词分析器】
- ElasticsearchCRUD使用(十二)【Elasticsearch的German分析器】
- ElasticsearchCRUD使用(十五)【批量插入数据】
- ElasticsearchCRUD使用(二)【简单的文档进行搜索的MVC应用程序】
- ElasticsearchCRUD使用(五)【Elasticsearch中的子文档,父文档】
- ElasticsearchCRUD使用(七)【Elasticsearch中的实时重建索引】
- ElasticsearchCRUD使用(十三)【Elasticsearch谷歌地图搜索的MVC应用】
- xx-net在chrome下请检查浏览器代理设置或不是私密连接解决办法
- linux下vi命令大全
- HashSet简单实现
- linux卸载系统自带jdk
- ORACLE的数据类型
- ElasticsearchCRUD使用(十四)【ElasticsearchCRUD搜索查询和过滤】
- React实战-FB最新开源Javascript优化利器Prepack
- ubuntu搭建LNMP环境(一)
- bzoj1207
- Grpc系列二 Grpc4种服务方法的定义和实现
- 反卷积(转置卷积)总结
- F——quadratic equation
- Ant 入门教程
- centos6更新yum源为阿里云