This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20947|Reply: 1

ElasticSearch compound queries must, should, must_not use

[Copy link]
Posted on 12/28/2018 12:01:09 PM | | |
bool joint query: must, should, must_not

If we want to request something similar to "content with BMW, but not in tag", we need to use bool federated query.

The joint query will use three keywords: must, should, must_not.

These three can be understood in this way

must: The document must match the conditions exactly
should: should have more than one condition, and if at least one condition is met, this document will meet should
must_not:The document must not match the criteria

Official website introduction:https://www.elastic.co/guide/cn/ ... bining-filters.html

Query DSL

match_all Query

All documents can be queried, and it is the default statement without query conditions.


This query is often used to merge filters. For example, you need to retrieve all mailboxes, and all document relevance is the same, so the _score you get is 1.

match query

A match query is a standard query that is basically used whether you need a full-text query or an exact query.

If you use match to query a full-text field, it will use the analyzer to parse the query characters before the actual query:



If you specify an exact value under match, it will search for your given value when it encounters a number, date, boolean, or not_analyzed string:


Tip: When doing an exact match search, it's best to use filter statements because filter statements cache data.

A match query can only search for an exact value that specifies an exact field, and all you have to do is specify the correct field name for it to avoid syntax errors.

multi_match Query

multi_match query allows you to search multiple fields at the same time based on a match query, and look up one in multiple fields at the same time:


bool query

Bool queries are similar to bool filtering and are used to merge multiple query clauses. The difference is that the bool filter can directly give whether the match is successful, while the bool query calculates the _score (correlation score) of each query clause.

must:: 查询指定文档一定要被包含。
must_not:: Query specified documents must not be included.
should:: 查询指定文档,有则可以为文档相关性加分。

The following query will find that the title field contains "how to make millions" and the "tag" field is not marked as spam. If they are labeled as "starred" or have a release date before 2014, then these matching documents will be at a higher level than similar sites:


Tip: If there is no must clause under the bool query, there should be at least one should clause. But if there is a must clause, then the query can also be made without the need clause.

wildcards query

Use standard shell wildcard queries

The following query matches documents containing W1F 7HW and W2F 8HW:


For example, the following query for hostname matches the following shell wildcard:


regexp query

Let's say you want to match only zip codes that start with W followed by numbers. Using regexp queries allows you to write more complex patterns:



This regular expression specifies that the entry must start with W, followed by a number from 0 to 9, followed by one or more other characters.

The following example is all regular starts with wxopen


prefix query

What character does it start with, you can use prefix more simply, as in the example below:


Phrase Matching

When you need to find a few words in close proximity, you use match_phrase query:


Similar to match queries, match_phrase query first parses the query string to produce a list of terms. It will search for all the entries,
However, only documents containing all search terms are kept, and the positions of the terms should be adjacent. A query for the phrase Quick Fox will not match
any of our documentation, as no documentation contains quick and box entries adjacent to each other.
match_phrase query can also be written as a match query of type phrase:







Previous:Purchased by China Accounting Online School in December 18 (Accounting Practice Level 6 Customs Clearance Training Camp)
Next:C# regex takes the value of the A tag href in HTML
Posted on 11/22/2022 2:25:30 PM |
Learn es, thank you landlord
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com