elasticsearch provides a full Java query dsl in a similar manner to the REST Query DSL. The factory for query builders is QueryBuilders
. Once your query is ready, you can use the Search API.
See also how to build Filters
To use QueryBuilders
just import them in your class:
import org.elasticsearch.index.query.QueryBuilders.*;
Note that you can easily print (aka debug) JSON generated queries using toString()
method on QueryBuilder
object.
The QueryBuilder
can then be used with any API that accepts a query, such as count
and search
.
Match Query
See Match Query
QueryBuilder qb =QueryBuilders.matchQuery("name","kimchy elasticsearch");
MultiMatch Query
See MultiMatch Query
QueryBuilder qb =QueryBuilders.multiMatchQuery( "kimchy elasticsearch", // Text you are looking for "user","message" // Fields you query on );
Boolean Query
See Boolean Query
QueryBuilder qb =QueryBuilders .boolQuery() .must(termQuery("content","test1")) .must(termQuery("content","test4")) .mustNot(termQuery("content","test2")) .should(termQuery("content","test3"));
Boosting Query
See Boosting Query
QueryBuilders.boostingQuery() .positive(QueryBuilders.termQuery("name","kimchy")) .negative(QueryBuilders.termQuery("name","dadoonet")) .negativeBoost(0.2f);
Custom Score Query
QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()) // Your query here .script("_score * doc['price'].value"); // Your script here // If the script have parameters, use the same script and provide parameters to it. QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()) .script("_score * doc['price'].value / pow(param1, param2)") .param("param1",2) .param("param2",3.1);
Custom Boost Factor Query
QueryBuilders.customBoostFactorQuery(QueryBuilders.matchAllQuery())// Your query .boostFactor(3.1f);
Constant Score Query
// Using with Filters QueryBuilders.constantScoreQuery(FilterBuilders.termFilter("name","kimchy")) .boost(2.0f); // With Queries QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name","kimchy")) .boost(2.0f);
Disjunction Max Query
QueryBuilders.disMaxQuery() .add(QueryBuilders.termQuery("name","kimchy")) // Your queries .add(QueryBuilders.termQuery("name","elasticsearch")) // Your queries .boost(1.2f) .tieBreaker(0.7f);
Field Query
See Field Query
QueryBuilders.fieldQuery("name","+kimchy -dadoonet"); // Note that you can write the same query using queryString query. QueryBuilders.queryString("+kimchy -dadoonet").field("name");
Fuzzy Like This (Field) Query (flt and flt_field)
See: * Fuzzy Like This Query * Fuzzy Like This Field Query
// flt Query QueryBuilders.fuzzyLikeThisQuery("name.first","name.last") // Fields .likeText("text like this one") // Text .maxQueryTerms(12); // Max num of Terms // in generated queries // flt_field Query QueryBuilders.fuzzyLikeThisFieldQuery("name.first") // Only on single field .likeText("text like this one") .maxQueryTerms(12);
Has Child / Has Parent
See: * Has Child Query * Has Parent
// Has Child QueryBuilders.hasChildQuery("blog_tag", QueryBuilders.termQuery("tag","something")) // Has Parent QueryBuilders.hasParentQuery("blog", QueryBuilders.termQuery("tag","something"));
More Like This (Field) Query (mlt and mlt_field)
See: * More Like This Query * More Like This Field Query
// mlt Query QueryBuilders.moreLikeThisQuery("name.first","name.last") // Fields .likeText("text like this one") // Text .minTermFreq(1) // Ignore Threshold .maxQueryTerms(12); // Max num of Terms // in generated queries // mlt_field Query QueryBuilders.moreLikeThisFieldQuery("name.first") // Only on single field .likeText("text like this one") .minTermFreq(1) .maxQueryTerms(12);
QueryString Query
QueryBuilder qb =QueryBuilders.queryString("+kimchy -elasticsearch");
Range Query
See Range Query
QueryBuilder qb =QueryBuilders .rangeQuery("price") .from(5) .to(10) .includeLower(true) .includeUpper(false);
Span Queries (first, near, not, or, term)
See: * Span First Query * Span Near Query * Span Not Query * Span Or Query * Span Term Query
// Span First QueryBuilders.spanFirstQuery( QueryBuilders.spanTermQuery("user","kimchy"), // Query 3 // Max End position ); // Span Near QueryBuilders.spanNearQuery() .clause(QueryBuilders.spanTermQuery("field","value1")) // Span Term Queries .clause(QueryBuilders.spanTermQuery("field","value2")) .clause(QueryBuilders.spanTermQuery("field","value3")) .slop(12) // Slop factor .inOrder(false) .collectPayloads(false); // Span Not QueryBuilders.spanNotQuery() .include(QueryBuilders.spanTermQuery("field","value1")) .exclude(QueryBuilders.spanTermQuery("field","value2")); // Span Or QueryBuilders.spanOrQuery() .clause(QueryBuilders.spanTermQuery("field","value1")) .clause(QueryBuilders.spanTermQuery("field","value2")) .clause(QueryBuilders.spanTermQuery("field","value3")); // Span Term QueryBuilders.spanTermQuery("user","kimchy");
Terms Query
See Terms Query
QueryBuilders.termsQuery("tags", // field "blue","pill") // values .minimumMatch(1); // How many terms must match
Top Children Query
QueryBuilders.topChildrenQuery( "blog_tag", // field QueryBuilders.termQuery("tag","something")// Query ) .score("max") // max, sum or avg .factor(5) .incrementalFactor(2);
Nested Query
See Nested Query
QueryBuilders.nestedQuery("obj1", // Path QueryBuilders.boolQuery() // Your query .must(QueryBuilders.matchQuery("obj1.name","blue")) .must(QueryBuilders.rangeQuery("obj1.count").gt(5)) ) .scoreMode("avg"); // max, total, avg or none
Custom Filters Score Query
See Custom Filters Score Query
QueryBuilders.customFiltersScoreQuery( QueryBuilders.matchAllQuery()) // Query // Filters with their boost factors .add(FilterBuilders.rangeFilter("age").from(0).to(10),3) .add(FilterBuilders.rangeFilter("age").from(10).to(20),2) .scoreMode("first"); // first, min, max, total, avg or multiply
Indices Query
See Indices Query
// Using another query when no match for the main one QueryBuilders.indicesQuery( QueryBuilders.termQuery("tag","wow"), "index1","index2" ) .noMatchQuery(QueryBuilders.termQuery("tag","kow")); // Using all (match all) or none (match no documents) QueryBuilders.indicesQuery( QueryBuilders.termQuery("tag","wow"), "index1","index2" ) .noMatchQuery("all"); // all or none
GeoShape Query
See GeoShape Query
Note: the geo_shape
type uses Spatial4J
and JTS
, both of which are optional dependencies. Consequently you must add Spatial4J
and JTS
to your classpath in order to use this type:
<dependency> <groupId>com.spatial4j</groupId> <artifactId>spatial4j</artifactId> <version>0.3</version> </dependency> <dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts</artifactId> <version>1.12</version> <exclusions> <exclusion> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> </exclusion> </exclusions> </dependency>
// Import Spatial4J shapes import com.spatial4j.core.context.SpatialContext; import com.spatial4j.core.shape.Shape; import com.spatial4j.core.shape.impl.RectangleImpl; // Also import ShapeRelation import org.elasticsearch.common.geo.ShapeRelation;
// Shape within another QueryBuilders.geoShapeQuery("location", newRectangleImpl(0,10,0,10,SpatialContext.GEO)) .relation(ShapeRelation.WITHIN); // Intersect shapes QueryBuilders.geoShapeQuery("location", newPointImpl(0,0,SpatialContext.GEO)) .relation(ShapeRelation.INTERSECTS); // Using pre-indexed shapes QueryBuilders.geoShapeQuery("location","New Zealand","countries") .relation(ShapeRelation.DISJOINT);
相关推荐
而Elasticsearch(ES)则是一款强大的全文搜索引擎,常用于大数据分析和实时搜索。本篇文章将详细介绍如何将Mybatis的Mapper方式与Elasticsearch的DSL(Domain Specific Language)查询相结合,以及如何通过CGlib...
**PyPI官网下载 | elasticsearch-dsl-5.3.0.tar.gz** `elasticsearch-dsl-5.3.0.tar.gz` 是一个在Python生态系统中的开源库,它源自Python Package Index (PyPI) 官方网站。这个压缩包包含了`elasticsearch-dsl`库...
Django Elasticsearch DSL Django Elasticsearch DSL是一个软件包,它允许在Elasticsearch中索引Django模型。 它是作为的薄包装而构建的,因此您可以使用elasticsearch-dsl-py团队开发的所有功能。 您可以在查看完整...
标题和描述提到了"django-elasticsearch-dsl-drf",这是一个用于集成Elasticsearch DSL(Domain Specific Language)与Django REST框架的库。这意味着它旨在帮助开发者在Django应用中轻松地使用Elasticsearch进行...
《PyPI上的django-elasticsearch-dsl-drf-0.13.2.tar.gz:整合Django、Elasticsearch与DRF的利器》 在Python的世界里,开发高效、可扩展的Web应用,Django框架是不可或缺的一部分。而随着大数据时代的发展,对数据...
本篇文章将详细介绍如何使用`elasticsearch-dsl`、`elasticsearch-py`和`django-elasticsearch-dsl`这三款库,在Django项目中实现Elasticsearch的集成。 首先,`elasticsearch-dsl`是Python的一个高级DSL(Domain ...
资源分类:Python库 所属语言:Python 资源全名:django-elasticsearch-dsl-drf-0.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Elasticsearch查询DSL支持100多种查询API,范围从全文搜索,数字范围过滤器,地理位置查询到嵌套查询和跨度查询。 Mirage是用于Elasticsearch的现代,开放源代码,基于Web的查询浏览器。 它提供了一个用于编写...
es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) ``` 3. **索引管理**:你可以使用 Elasticsearch 客户端来创建、更新或删除索引。索引是存储和检索文档的主要容器,每个文档都属于某个特定的索引。 `...
**Python-elasticsearch-dsl-py:Elasticsearch的官方高级Python客户端** Elasticsearch 是一个分布式、全文搜索引擎,常用于实时数据分析和大规模数据检索。它以其高效、灵活和可扩展性而闻名。为了方便Python...
**Elasticsearch插件Delete-by-Query详解** 在Elasticsearch中,删除操作通常是针对单个文档进行的,但有时我们需要删除满足特定条件的一大批文档。这时,`Delete-by-Query`插件就显得尤为重要。它允许我们通过一个...
在这篇资源中,我们将详细介绍如何使用DSL...最后,我们将分享一些实用技巧和最佳实践,帮助您充分发挥DSL在Elasticsearch中的优势。我们将探讨性能优化、查询调试和索引优化等关键主题,以提升查询效率和搜索准确性。
Splunk-SPL-to-ElasticSearch-DSL 基于 Splunk 的 SPL 查询语言转换成 ElasticSearch 的 DSL。 转换结果和 对齐。 可以配置 进行表达式搜索。 Usage const converter = require("./lib/converter") try { const { ...
弹性搜索DSL 引入Elasticsearch DSL库以为和客户端提供客观的查询构建器。 您可以轻松构建任何Elasticsearch查询并将其转换为数组。 如果您需要任何帮助,则是询问ONGR支持问题的首选和推荐方法。 如果您喜欢这个...
总的来说,`elasticsearch-dsl.cr`是Crystal开发者与Elasticsearch交互的强大工具,它降低了Elasticsearch API的使用门槛,提高了代码质量,让开发工作更加高效和愉快。通过深入理解和熟练运用这个库,你将能够更好...
用于弹性搜索的 DSL 动机 近年来软件领域的一个新兴趋势,包括 mongodb、elasticsearch 和 Chef,都是公开一个 JSON 接口来接受复杂的... 只是json.dumps(query_py_obj) ,你会得到 JSON 请求到 elasticsearch neste
8. **查询DSL (Query DSL)**: 详细介绍了ElasticSearch的查询语言(Domain Specific Language),包括全量查询(Match All Query)、全文查询(Full Text Queries)、词项级查询(Term level queries)、复合查询...
If you (or others you know) are versed in SQL statements and newer to the Elasticsearch query syntax but want to benefit from the power Elasticsearch, this is the talk for you. After making its debut ...
5. **查询接口设计**:利用 Elasticsearch 的 Query DSL 设计查询接口,支持多种查询条件和组合。 6. **结果排序与聚合**:实现搜索结果的排序和聚合统计,以提供更丰富的查询结果展示。 7. **监控与调优**:通过 ...
Elasticsearch(简称ES)是一款强大的开源搜索引擎,广泛应用于数据检索、分析和管理。作为分布式、RESTful风格的搜索和数据分析引擎,Elasticsearch能够提供实时、高可用性以及可扩展的搜索功能。在进行日常的数据...