Before introducing this chapter, you need to open the Kibana page of installation and the command line tools page:
According to the introduction in the previous section, we need to search according to Chinese. Therefore, when creating a mapping, we need to specify the word splitter of Chinese field as Ik word splitter, which defaults to English word splitter. Each Chinese word needs to rebuild the index and rebuild the mapping. First, execute the following commands from the command line:
# Delete index DELETE course # Build course index PUT course # Specify a mapping for course and a word breaker for the analyzer attribute PUT course/_mapping { "properties": { "name": { "type": "text", "analyzer":"ik_max_word" }, "description": { "type": "text", "analyzer":"ik_max_word" }, "studymodel": { "type": "keyword" } } } # Save three test documents POST /course/_doc { "name": "Bootstrap Development framework", "description": "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel": "201001" } POST /course/_doc { "name": "Java language", "description": "Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use", "studymodel": "201002" } POST /course/_doc { "name": "Spring frame", "description": "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel": "201003" }
1. Introduction to common search
1.1 search all
Command: GET course/_search
result:
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.0, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "P2xaGX0BjGPlWFrZF6_S", "_score" : 1.0, "_source" : { "name" : "Java language", "description" : "Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use", "studymodel" : "201002" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 1.0, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ] } }
The returned result includes all three documents and is placed in the array hits. A search returns ten results by default.
1.2 match word segmentation search
Domain specific language (DSL) constructs a request using JSON. We can query all documents whose studymodel is "201003" like this:
GET course/_search { "query": { "match": { "description": "frame" } } }
result:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.6502306, "hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 0.6502306, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 0.43878573, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ] } }
The request is constructed using JSON, and the outermost part contains a "query" attribute, which is required by the query statement
A match query is used internally to match the document with the word "frame" in the description attribute;
Principle:
We know that when adding a new document, the field of text type will be split and segmented according to the type and word segmentation type, and the disassembled words will be saved in the index thesaurus for matching. If they are matched, the document corresponding to this word will be queried, that is, the inverted index
The match query will first analyze the query conditions, segment the query conditions (use the same word splitter as the field, or specify it yourself), and then query
In this example, the search term is "frame", and the separated words are also "frame"; Therefore, the document with the word "frame" in the description field of the document is queried
Match multiple words:
GET course/_search { "query": { "match": { "description": "Frame interface" } } }
It can be seen that the words searched this time are no longer single words, but are composed of "framework" and "interface". During the search, the search statement will also be split into these two words for matching:
result:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.6398609, "hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.6398609, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 0.43878573, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ] } }
It can be seen from the results that the word "frame" exists in both documents, but only one has the word "interface", so it can be judged that the word matching is the relationship between or, as long as there is one matching, but according to the matching degree, the one with high matching degree ranks first; If no word is matched, it is not queried;
1.2.1 operator attribute
In the above word segmentation matching, we found that as long as one of the words in the word segmentation is matched in the document, it is a match
This is because the operator attribute of the default match match is or, that is, the relationship between or and can also be modified to and, that is, each word must match:
GET course/_search { "query": { "match": { "description": { "query": "Frame interface", "operator": "and" } } } }
result:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.6398609, "hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.6398609, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } } ] } }
The same search is for this word, but the only matching result is the document on which each word matches
1.2.2 minimum_should_match attribute
The operator = or used above indicates that as long as there is one word matching, the score will be scored. If three words are matched, how can we achieve at least two words matching?
Using minimum_should_match can specify the proportion of document matching words
For example, the search statements are as follows:
GET course/_search { "query": { "match": { "description": { "query": "Launch frame interface", "operator": "or", "minimum_should_match": "80%" } } } }
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 2.629491, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } } ]
"Launch framework interface" will be divided into three words: launch, framework and interface
Set "minimum_should_match": "80%" means that the matching proportion of three words in the document is 80%, that is, 3 * 0.8 = 2.4, rounded down to 2, table
Show that at least two words in the document should be matched successfully.
1.3 multi Query matching multiple fields
The matchQuery learned above can only match one Field at a time. This section studies multiQuery, which can match multiple fields at a time.
GET course/_search { "query": { "multi_match": { "query": "Launch frame interface", "fields":["name","description"], "operator": "or", "minimum_should_match": "50%" } } }
Specify the operator and minimum that match the name and description fields_ should_ The match attribute will act on the matching of each field. As long as one field meets the conditions, the whole expression holds:
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 2.629491, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 0.49917626, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
Results two results were queried,
The name and description attributes of the first result meet 50% of the matching criteria
The name attribute of the second result matches the "frame character", 50%, rounding down is a word, which is also a match
1.4 term exact match search
Term Query is an accurate query. It will match keywords as a whole during search, and keyword segmentation is no longer required.
Similarly, we use the term statement to query the word "framework interface", which will no longer match any document:
GET course/_search { "query": { "term": { "description": "Frame interface" } } }
result:
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }
This is because when using term search, the search terms will no longer be segmented, but will be accurately matched. However, the original text does not divide the "framework interface" into a single word and put it into the index library
Therefore, term query is generally used for accurate query, such as:
GET course/_search { "query": { "term": { "studymodel": "201003" } } }
Query the course No. 2001003, and the result is:
"hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.9808291, "hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 0.9808291, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
Alternatively, you can use terms to match multiple
GET course/_search { "query": { "terms": { "studymodel": ["201003","201001"] } } }
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.0, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 1.0, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
1.4 fuzzy matching
Wildcard wildcard query is also an underlying word based query, which uses standard shell wildcard query:? Matches any character, * matches 0 or more characters. Similar to like query in SQL
GET course/_search { "query": { "wildcard": { "studymodel": "201*" } } }
Query all courses with numbers beginning with "201", and the results will find all:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.0, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "P2xaGX0BjGPlWFrZF6_S", "_score" : 1.0, "_source" : { "name" : "Java language", "description" : "Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use", "studymodel" : "201002" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 1.0, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
If there are more complex fuzzy matching requirements, regular matching can also be used:
GET course/_search { "query": { "regexp": { "studymodel": "201[0-9].3+" } } }
Match the numbered courses starting with 201, ending with 3 and all composed of numbers. The query results are as follows:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 1.0, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
be careful:
The working mode of wildcard and regexp query needs to scan the word list in the inverted index to find all matching words, and then obtain the document ID related to each word in turn, which is relatively resource-consuming and time-consuming
This also means that you need to pay equal attention to the problems of prefix queries. Executing these queries on fields with many unique words may consume a lot of resources, so you should avoid using pattern matching such as left wildcard (such as * foo or. * foo)
wildcard and regexp queries are word based. If they are used to query the analyzed field, they will check each word in the field instead of processing the field as a whole.
For example, the title field containing "Quick brown fox" will generate the words: quick, brown and fox.
The following query will be matched:
{ "regexp": { "title": "br.*" }}
However, the following two queries will not be matched:
{ "regexp": { "title": "Qu.*" }} { "regexp": { "title": "quick br*" }}
The word in the index is quick, not quick.
quick and brown are separated in the thesaurus.
1.5 Boolean query
Boolean query corresponds to Lucene's Boolean query, which realizes the combination of multiple queries. Similar to or or and in SQL
Three parameters:
Must: the document must match the query criteria included in must, which is equivalent to "AND"
Should: the document should match the query criteria included in should, which should be "OR"
must_not: document cannot match must_ The query criteria included in NOT is equivalent to "NOT"
Use must, should and must respectively_ Not test the following query:
1.5.1 must
GET course/_search { "query": { "bool": { "must": [ { "match": { "description": "program" } }, { "match": { "name": "frame" } } ] } } }
As shown in the expression, the bool expression is used, including two match queries (the sub query type doesn't matter, and the bool query only takes the final result for each result)
The description and name fields should match the corresponding characters at the same time
Only one result is satisfied:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.777754, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } } ]
1.5.2: should
GET course/_search { "query": { "bool": { "should": [ { "match": { "description": "program" } }, { "match": { "name": "frame" } } ] } } }
Two expressions, as long as one satisfies
The results match two conditions:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.777754, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : 0.49917626, "_source" : { "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from", "studymodel" : "201003" } } ]
At the same time, the "minimum_should_match" attribute can also be used here to define the number of items to be matched. By default, it is equivalent to 1, that is, one result in the expression can be satisfied. If it is set to 2, it means that all items need to be satisfied, which is equivalent to must:
GET course/_search { "query": { "bool": { "should": [ { "match": { "description": "program" } }, { "match": { "name": "frame" } } ] , "minimum_should_match": 2 } } }
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.777754, "_source" : { "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser", "studymodel" : "201001" } } ]
1.5.3 must_not
It is equivalent to the negation of the should expression, that is, none of the expressions can match:
GET course/_search { "query": { "bool": { "must_not": [ { "match": { "description": "program" } }, { "match": { "name": "frame" } } ] } } }
The result is that name does not contain the word program and the description does not contain the word framework:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "P2xaGX0BjGPlWFrZF6_S", "_score" : 0.0, "_source" : { "name" : "Java language", "description" : "Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use", "studymodel" : "201002" } } ]
1.6 range query
Before the introduction, you need to add a price field of numerical type and execute the following command:
# Add a field and specify the type as float PUT course/_mapping { "properties": { "price": { "type": "float" } } } #Modify the price according to the number: POST course/_update_by_query { "script": { "source": "ctx._source['price']=10.5;" }, "query":{ "term": { "studymodel": { "value": "201001" } } } } POST course/_update_by_query { "script": { "source": "ctx._source['price']=5.6;" }, "query":{ "term": { "studymodel": { "value": "201002" } } } } POST course/_update_by_query { "script": { "source": "ctx._source['price']=20.3;" }, "query":{ "term": { "studymodel": { "value": "201003" } } } }
For numbers, except for exact value queries. In fact, filtering is sometimes more useful. For example, we may want to find all courses with a price greater than 10 and less than 20 yuan. You can use range queries:
GET course/_search { "query": { "range": { "price": { "gte": 10, "lte": 20 } } } }
Query results:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 1.0, "_source" : { "price" : 10.5, "studymodel" : "201001", "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser" } } ]
Range query can provide both inclusive and exclusive range expressions. The options for combination are as follows:
- GT: > greater than
- LT: < less than
- GTE: > = greater than or equal to
- LTE: < = less than or equal to
2. Sorting
When searching with the query keyword, by default, the documents are sorted according to the score of each document, that is, the matching degree of the document to the search criteria (_scorefield)
You can also specify the result sorting, so that the score will not be calculated during query
Next, you can query 5 ~ 30 courses according to the price, in descending order
GET course/_search { "query": { "range": { "price": { "gte": 5, "lte": 30 } } } , "sort": [ { "price": { "order": "asc" } } ] }
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "P2xaGX0BjGPlWFrZF6_S", "_score" : null, "_source" : { "price" : 5.6, "studymodel" : "201002", "name" : "Java language", "description" : "Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use" }, "sort" : [ 5.6 ] }, { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : null, "_source" : { "price" : 10.5, "studymodel" : "201001", "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser" }, "sort" : [ 10.5 ] }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : null, "_source" : { "price" : 20.3, "studymodel" : "201003", "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from" }, "sort" : [ 20.3 ] } ]
If you need to sort by multiple fields, similar to mysql, you can also specify multiple fields. If the first field is the same, sort by the second field:
GET course/_search { "query": { "range": { "price": { "gte": 5, "lte": 30 } } } , "sort": [ { "price": { "order": "asc" } }, { "studymodel": { "order": "asc" } } ] }
3. Pagination
ES supports paging query. Two parameters are passed in: from and size. The two parameters are the same as the limit in mysql
form: indicates the subscript of the starting document, starting from 0.
size: number of documents queried.
After sorting below, query from subscript 1 and query two
GET course/_search { "query": { "range": { "price": { "gte": 5, "lte": 30 } } }, "from":1, "size":2 , "sort": [ { "price": { "order": "asc" } }, { "studymodel": { "order": "asc" } } ] }
The results did not show the first lowest price item
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : null, "_source" : { "price" : 10.5, "studymodel" : "201001", "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter Launched a front page development framework, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser" }, "sort" : [ 10.5, "201001" ] }, { "_index" : "course", "_type" : "_doc", "_id" : "QGxjGX0BjGPlWFrZka88", "_score" : null, "_source" : { "price" : 20.3, "studymodel" : "201003", "name" : "Spring frame", "description" : "Spring The framework is created due to the complexity of software development. Spring The use is basic JavaBean Can only be done by EJB Things done. However, Spring Its purpose is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java Applications are available from Spring Benefit from" }, "sort" : [ 20.3, "201003" ] } ]
4. Filter
The query language (DSL) used by elastic search has a set of query components that can be matched in an infinite combination.
This set of components can be used in the following two cases: filtering context and query context.
When used in filter, the query is set as a "no score" or "filter" query. That is, the query simply asks a question: "does this document match?". The answer is also very simple. yes or no must be one of the two.
- Is the created time between 2013 and 2014?
- Does the status field contain the word published?
- lat_ Is the position represented by the lon field within 10km of the specified point?
When used in a query, the query becomes a "scoring" query. Similar to the query without scoring, it also needs to judge whether the document matches. At the same time, it also needs to judge how well the document matches (how well it matches). A typical use of this query is to find the following documents:
- Find the document that best matches the word full text search
- Including the word run can also match runs, running, jog or sprint
- Include the words quick, brown, and fox - the closer the words are, the more relevant the document is
- Tagged with lucene, search, or java - the more tags, the more relevant
Performance differences:
Filter queries simply check for inclusion or exclusion, which makes the calculation very fast. And the results are cached in memory
For fast reading, there are various means to optimize query results.
On the contrary, scoring query not only finds the matching documents, but also calculates the correlation of each matching document. Calculating the correlation makes them much more laborious than non scoring query. At the same time, query results are not cached.
Thanks to the inverted index, a simple scoring query may perform as well or even better when matching a small number of documents as a filter covering millions of documents. However, in general, a filter will perform better than a rated query, and the performance is very stable every time.
The general rule is to use query statements for full-text search or any other search that needs to affect the relevance score. All other cases use filters.
use:
Since the advent of Elasticsearch, queries and filters have become the components of Elasticsearch alone.
However, since Elasticsearch 2.0, filters have been technically excluded. All our queries above are also based on the query keyword. However, in the bool query, we can use the attribute filter other than must, shy and must_not
For the following example, match the description field, and then match the studymodel and price fields using filter. The meaning of filter is the same as must, which means that all conditions in the filter need to be met
GET course/_search { "query": { "bool": { "must": [{ "match": { "description": { "query": "frame" } } }], "filter": [{ "term": { "studymodel": "201001" } }, { "range": { "price": { "gte": 5, "lte": 20 } } } ] } } }
result:
"hits" : [ { "_index" : "course", "_type" : "_doc", "_id" : "PmxaGX0BjGPlWFrZEK_N", "_score" : 0.6502306, "_source" : { "price" : 10.5, "studymodel" : "201001", "name" : "Bootstrap Development framework", "description" : "Bootstrap By Twitter A foreground page development framework is launched, which is widely used in the industry. This development framework contains a large number of CSS, JS Program code can help developers (especially those who are not good at page development) easily realize a beautiful interface effect that is not limited by the browser" } } ]