ES权威指南_01_get start_07 Full-Body Search
来源:互联网 发布:c语言计算闰年循环 编辑:程序博客网 时间:2024/06/13 02:01
https://www.elastic.co/guide/en/elasticsearch/guide/current/full-body-search.html
1 Empty Search
returns all documents in all indices:
GET /_search{}
you can search on one, many, or _all indices, and one, many, or all types:
GET /index_2014*/type1,type2/_search{}
use the from and size parameters for pagination:
GET /_search{ "from": 30, "size": 10}
A GET Request with a Body?
The authors of Elasticsearch prefer using GET
for a search request because they feel that it describes the action—retrieving information—better than the POST
verb. However, because GET with a request body is not universally supported, the search API also accepts POST requests:
POST /_search{ // query DSL "from": 30, "size": 10}
2 Query DSL
flexible, expressive search language
GET /_search{ "query": YOUR_QUERY_HERE //query _all}
The empty search—{}—is functionally equivalent to using the match_all query clause
GET /_search{ "query": { "match_all": {} }}
GET /_search{ "query": { "match": { "tweet": "elasticsearch" } }}
Combining Multiple Clauses
{ "bool": { "must": { "match": { "tweet": "elasticsearch" }}, "must_not": { "match": { "name": "mary" }}, "should": { "match": { "tweet": "full text" }}, "filter": { "range": { "age" : { "gt" : 30 }} } }}
It is important to note that a compound clause can combine any other query clauses, including other compound clauses.
{ "bool": { "must": { "match": { "email": "business opportunity" }}, "should": [ { "match": { "starred": true }}, { "bool": { "must": { "match": { "folder": "inbox" }}, "must_not": { "match": { "spam": true }} }} ], "minimum_should_match": 1 }}
3 Queries and Filters
two contexts: filtering context and query context.
filtering context, the query is said to be a “non-scoring” or “filtering” query,yes|no.
querying context, the query becomes a “scoring” query , if a document matches and how well the document matches.
注意:
Historically, queries and filters were separate components in Elasticsearch. Starting in Elasticsearch 2.0, filters were technically eliminated, and all queries gained the ability to become non-scoring.
However, for clarity and simplicity, we will use the term “filter” to mean a query which is used in a non-scoring, filtering context. You can think of the terms “filter”, “filtering query” and “non-scoring query” as being identical.
Similarly, if the term “query” is used in isolation without a qualifier, we are referring to a “scoring query”.
Performance Differences
Filtering queries are simple checks for set inclusion/exclusion, which make them very fast to compute. There are various optimizations that can be leveraged when at least one of your filtering query is “sparse” (few matching documents), and frequently used non-scoring queries can be cached in memory for faster access.
In contrast, scoring queries have to not only find matching documents, but also calculate how relevant each document is, which typically makes them heavier
Thanks to the inverted index, a simple scoring query that matches just a few documents may perform as well or better than a filter that spans millions of documents. In general, however, a filter will outperform a scoring query.
The goal of filtering is to reduce the number of documents that have to be examined by the scoring queries.
4 Most Important Queries
4.1 match_all
{ "match_all": {}}
4.2 match
exact value 或 full-text
{ "match": { "age": 26 }}{ "match": { "tweet": "About Search" }}
提示:For exact-value searches, you probably want to use a filter clause instead of a query, as a filter will be cached.
4.3 multi_match
run the same match query on multiple fields:
{ "multi_match": { "query": "full text search", "fields": [ "title", "body" ] }}
4.4 rang
gt,gte,lt,lte
{ "range": { "age": { "gte": 20, "lt": 30 } }}
4.5 term
used to search by exact values,, be they numbers, dates, Booleans, or not_analyzed exact-value string fields
{ "term": { "age": 26 }}
The term query performs no analysis on the input text, so it will look for exactly the value that is supplied.
4.6 terms 【任一】
specify multiple values to match.
If the field contains any of the specified values, the document matches:
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}
4.7 exists and missing
{ "exists": { "field": "title" }}
5 Combining queries together
must
must_not
should,If these clauses match, they increase the _score; otherwise, they have no effect.
filter ,must match, but are run in non-scoring, filtering mode.
提示:
If there are no must clauses, at least one should clause has to match. However, if there is at least one must clause, no should clauses are required to match.
Adding a filtering query
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }} ], "filter": { "range": { "date": { "gte": "2014-01-01" }} } }}
If you need to filter on many different criteria, the bool query itself can be used as a non-scoring query.
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }} ], "filter": { "bool": { "must": [ { "range": { "date": { "gte": "2014-01-01" }}}, { "range": { "price": { "lte": 29.99 }}} ], "must_not": [ { "term": { "category": "ebooks" }} ] } } }}
constant_score Query
The query applies a static, constant score to all matching documents.
You can use this instead of a bool that only has filter clauses. Performance will be identical, but it may aid in query simplicity/clarity.
{ "constant_score": { "filter": { "term": { "category": "ebooks" } } }}
A term query is placed inside the constant_score, converting it to a non-scoring filter.
6 Validating Queries
check whether a query is valid.
GET /gb/tweet/_validate/query{ "query": { "tweet" : { // 和match颠倒了 "match" : "really powerful" } }}{ "valid" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }}
Understanding Errors
GET /gb/tweet/_validate/query?explain {...}{ "valid" : false, "_shards" : { ... }, "explanations" : [ { "index" : "gb", "valid" : false, "error" : "org.elasticsearch.index.query.QueryParsingException: [gb] No query registered for [tweet]" } ]}
Understanding Queries
GET /_validate/query?explain{ "query": { "match" : { "tweet" : "really powerful" } }}{ "valid" : true, "_shards" : { ... }, "explanations" : [ { "index" : "us", "valid" : true, "explanation" : "tweet:really tweet:powerful" }, { "index" : "gb", "valid" : true, "explanation" : "tweet:realli tweet:power" } ]}
- ES权威指南_01_get start_07 Full-Body Search
- ES权威指南_01_get start_01 You Know, for Search…
- ES权威指南_01_get start_09 Distributed Search Execution
- ES权威指南_01_get start_10 Index Management
- ES权威指南_01_get start_03 Data In, Data Out
- ES权威指南_01_get start_04 Distributed Document Store
- ES权威指南_01_get start_05 Searching—The Basic Tools
- ES权威指南_01_get start_06 Mapping and Analysis
- ES权威指南_01_get start_08 Sorting and Relevance
- ES权威指南_01_get start_11 Inside a Shard
- ES权威指南_01_get start_02 Life Inside a Cluster(ES集群内部原理)
- Elastic Search权威指南 第一章 ES基础概念
- ES权威指南_05_Geolocation_02 Geohashes
- ES权威指南_04_aggs_01 High-Level Concepts
- ES权威指南_04_aggs_05 Scoping Aggs
- ES权威指南_04_aggs_07 Sorting Multivalue Buckets
- ES权威指南_04_aggs_11 Closing Thoughts
- ES权威指南_05_Geolocation_01 Geo Points
- Mac下安装mysql5.7 完整步骤(图文详解)
- 机器学习-Logistic回归之梯度上升法
- 编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,及将句子中的单词位置倒置,而不改变单词内部结构.
- 算法之路二:刘汝佳算法竞赛入门经典 3.10环状序列 UVa1584
- 易经入门概述
- ES权威指南_01_get start_07 Full-Body Search
- Hibernate中类级别和关联级别加载策略
- Initializing Spring root WebApplicationContext
- 艳辉网V1.0之VOD点播系统
- JavaWeb,Struts2框架入门,MyEclipse下环境配置,404错误解决
- CSS | 渐变的妙不可言 linear & radial
- ES权威指南_01_get start_08 Sorting and Relevance
- 启动项目出现:java.lang.NoClassDefFoundError: org/springframework/asm/ClassVisitor
- qq个人中心g_tk算法