Elasticsearch7.X entry-learning Lesson notes ---- Search API's (Request Body Search and DSL Profile)

Original: Elasticsearch7.X entry-learning Lesson notes ---- Search API's (Request Body Search and DSL Profile)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_36697880/article/details/100654455

Elasticsearch suggested that we try to use Request Body query syntax in this way to support richer.

Common Syntax

     First look at a simple example. This example queries two indexes (movies and 404_idx) of all documents. Theoretically, if 404_idx index does not exist, Elasticsearch returns an error. However, due to ignore_unavailable parameters, execution will ignore the index unavailable.


   
   
  1. # ignore_unavailable= true,可以忽略尝试访问不存在的索引“ 404_idx”导致的报错
  2. POST /movies, 404_idx/_search?ignore_unavailable= true
  3. {
  4. "profile": true,
  5. "query": {
  6. "match_all": {}
  7. }
  8. }

 1 from / sizepagination

    We can also use  from / size to implement paging combination.


   
   
  1. POST /movies/_search
  2. {
  3. "from": 0,
  4. "size": 20,
  5. "query": {
  6. "match_all": {}
  7. }
  8. }

2 sortSorting

     Use sortto achieve the sort. If you need something like sql statement the same sort on multiple fields, multiple elements can be passed in the sort inside.


   
   
  1. POST /movies/_search
  2. {
  3. "sort": [{ "year": { "order": "desc"}},
  4. { "id.keyword": { "order": "desc"}} ],
  5. "from": 0,
  6. "size": 20,
  7. "query": {
  8. "match_all": {}
  9. }
  10. }

3 _source filter

 Too many results Elasticsearch query, it will affect the efficiency of execution. Therefore, we can reduce query fields by filtration _source way.

  The following query to display only the title attribute


   
   
  1. POST /movies/_search
  2. {
  3. "sort": [{ "year": { "order": "desc"}},
  4. { "id.keyword": { "order": "desc"}} ],
  5. "from": 0,
  6. "size": 20,
  7. "_source": [ "title"],
  8. "query": {
  9. "match_all": {}
  10. }
  11. }

4 Script field

  Elasticsearch also supports painless script field, this mechanism can do some simple operations by way of the script, such as: string concatenation. In order scenario, orders have different exchange rates, calculated by the script, we can sort the results for the calculation.

  


   
   
  1. POST /movies/_search
  2. {
  3. "sort": [{ "year": { "order": "desc"}},
  4. { "id.keyword": { "order": "desc"}} ],
  5. "from": 0,
  6. "size": 20,
  7. "query": {
  8. "match_all": {}
  9. }
  10. , "script_fields": {
  11. "new_title": {
  12. "script": {
  13. "lang": "painless",
  14. "source": "doc['year'].value+'hello'"
  15. }
  16. }
  17. }
  18. }

5 match expression statement

 1) query match, the contents of the query is OR default way. As follows:


   
   
  1. POST /movies/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "last christmas"
  6. }
  7. }
  8. }

 2) If you are using AND manner, may be achieved by specifying the operator is and.


   
   
  1. POST /movies/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "last christmas",
  7. "operator": "and"
  8. }
  9. }
  10. }

6 phrase queries 

   Use the phrase queries. Namely:phrase query . No gaps among phrases default, but may be used to represent intermediate slop = 1 may be spaced a Term (word)


   
   
  1. POST /movies/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title":{
  6. "query": "one love",
  7. "slop": 1
  8. }
  9. }
  10. }
  11. }

二 Query string && Simple query string

    1 Elasticsearch also supports query string and simple query string. Let's look query string, in this way supports packet and multi-field.

 First, two test data insert


   
   
  1. PUT /users/_doc/ 1
  2. {
  3. "name": "Ruan Yiming",
  4. "about": "java, golang, node, swift, elasticsearch"
  5. }
  6. PUT /users/_doc/ 2
  7. {
  8. "name": "Li Yiming",
  9. "about": "Hadoop"
  10. }

test:


   
   
  1. POST /users/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "default_field": "name",
  6. "query": "Ruan AND Yiming"
  7. }
  8. }
  9. }
  10. # 多字段 使用 fields
  11. POST /users/_search
  12. {
  13. "query": {
  14. "query_string": {
  15. "fields": [ "name", "about"],
  16. "query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
  17. }
  18. }
  19. }

simple query string, similar to the query string, but with the following differences:

  1. Grammar errors are ignored, and only supports parts of the query syntax
  2. Does not support AND, , OR, NOTif the term appears, will be treated as a string
  3. term relationship between the default OR, you can specify default_operator
  4. Support part of the logic, +instead of AND, |instead of OR, -instead ofNOT

   
   
  1. POST users/_search
  2. {
  3. "query": {
  4. "simple_query_string": {
  5. "query": "Zhang Fubing",
  6. "fields": [ "name"],
  7. "default_operator": "AND"
  8. }
  9. }
  10. }

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11612432.html