Learning the grammar of Elasticsearch DSL

Keywords: Programming

DSL Grammar Learning

(1)term and terms queries
(2)match query

match_all: Query all documents

multi_match: Multiple fields can be specified

match_phrase: Phrase Matching Query

(3)rang Range Query
(4)wildcard Query

Allow wildcards * and?To query

* Represents 0 or more characters

What?Represents any character

(5)fuzzy Query

value: the keyword for the query

boost: The weight of the query, default 1.0

(6)highlight highlighting

fields

(7)bool query

must: The condition satisfied is--- and

should: Swords that can or cannot be satisfied-----or

must_not: Unnecessary condition--not

(8) Aggregate queries

Sum: sum

avg: average

count: statistics

cardinality: value de-counting

<hr/>

Query: GET

GET/_search{ "query":{"term":{"user":"kimchy"}}}

Query document

#Inverse age Query 
POST/pigg/_search
{
  "query": {"match_all": {}},
  "sort": [
    {"age": {"order": "desc"}}
  ]
}
#Query the first 2 data, from 0 
POST/pigg/_search
{
	"query": {"match_all": {}},
	"sort": [
		{"age" :{"order": "desc"}
	],
	"from": 0,
	"size": 2
}
Keyword Query (Exact Query)
POST/aegis/positions/_search
{
	"query": {
		"term": {
			"title": "meter"
		}
	}
}
Word Segmentation Query
POST/aegis/positions/_search
{
	"query":{
		"match":{
			"title": "millet"
		}
	}
}

(_source Source, filter settings, specify which fields are included in the result)
POST/aegis/positions/_search   
{
	"query":{
		"match":{
			"title":"millet"
		}
	},
 "_source":["title","price"]
}
Boolean query title is a word and brand is exact
POST/aegis/positions/_search 
{
	"query":{
	 	"bool":{
	 		"must":[
	 			{
	 				"match":{
	 					"title": "millet"
	 				}
	 			},
	 			{
	 				"term":{
	 					"brand": "rice"
	 				}
	 			}
	 		]
	 	}
	}
}

Filter: Filtering is to filter the results of a search. The main judgment of the filter is whether the documents match or not.

Filter results
POST/aegis/positions/_search 
{
	"query":{
	 	"bool":{
	 		"must":[
	 			{
	 				"match":{
	 					"title":"millet"
	 				}
	 			},
	 			{
	 				"term":{
	 					"brand":"rice"
	 				}
	 			}
	 		],
            "filter":[
                {
                     "range":{
                        "price":{
                            "gte":3298
                        }
                    }
                }
            ]
	 	}
	}
}
Sort after filtering
POST/aegis/positions/_search 
{
	"query":{
	 	"bool":{
	 		"must":[
	 			{
	 				"match":{
	 					"title":"millet"
	 				}
	 			},
	 			{
	 				"term":{
	 					"brand":"rice"
	 				}
	 			}
	 		],
            "filter":[
                {
                     "range":{
                        "price":{
                            "gt":3298
                        }
                    }
                }
            ]
	 	}
	},
    "sort":[
	 		{
                "price":"desc"
	 		}
	 	]
}
Highlight
{
	"query":{
	 	"bool":{
	 		"must":[
	 			{
	 				"match":{
	 					"title":"millet"
	 				}
	 			},
	 			{
	 				"term":{
	 					"brand":"rice"
	 				}
	 			}
	 		],
            "filter":[
                {
                     "range":{
                        "price":{
                            "gt":3298
                        }
                    }
                }
            ]
	 	}
	},
    "sort":[
	 		{
                "price":"desc"
	 		}
	 ],
    "highlight":{
        "pre_tags":["<tag1>"],
        "post_tags":["</tag2>"],
        "fields":{
            "title":{},
            "brand":{}
        }
    }
}
term query query is suitable for keyword, numeric, data
terms, multiple keyword queries        
{
    "query":{
        "terms":{
            "title": ["meters", "for"]
        }
    }
}

Return version number:'version':

PhraseQuery
match_phrase_prefix prefix
{
    "query":{
        "match_phrase":{
            "title": "millet"
        }
    }
}
WildcardQuery
{
    "query":{
        "wildcard":{
            "title": "small*"
        }
    }
}
Fuzzy Query
{
    "query":{
        "fuzzy":{
            "title":{
             	"value": "millet"   
            }
        }
    }
}
{
    "query":{
      	"match":{
            "title":"millet"
        }
    },
     "highlight":{
            "fields":{
                "title":{}
            }
        }
}
Total price of aggregated query price
{
    "aggs":{
        "price_sum":{
            "sum":{
                "field":"price"
            }
        }
    }
}


Aggregate query calculates quantity based on price
{ 
    "size":0,
    "aggs":{
        "price_sum":{
            "terms":{
                "field":"price"
            }
        }
    }
}
Aggregate queries 
{
    "query":{
        "range":{
            "price":{
                "gt":2799
            }
        }
    },
    "aggs":{
        "brandGroup":{
            "terms":{
                "field":"brand.keyword"
            },
            "aggs":{
                "priceAVG":{
                    "avg":{
                        "field":"price"
                    }
                }
            }
        }
    }
}
Aggregate queries:
Brand error, FeildData field disabled, set fielddata=true, or add brand.keyword
{
   "query":{
       "match":{
         "title": "millet"
 		}
  	},
   "aggs":{ 
      "all_tags":{
          "terms":{
           "field":"brand.keyword"
     		}
 		}
 	}
}		
Average Aggregate Queries, Give Value + 10
{
    "aggs":{
        "avg_price":{
            "avg":{
                "script":{
                    "source":"doc.price.value + 10"
                }
            }
        }
    }
}
(9) Nested Query:Nested
Create Nested Objects mapping
PUT /earth_index
{
    "mappings":{
        "_doc":{
            "properties":{
                "title":	{"type":"text"},
                "body":	{"type":"text"},
                "comments":{
                    "type":"nested",
                   	"properties":{
                        "name":	{"type":"text"},
                        "comment":	{"type":"text"},
                        "age":	{"type":"short"},
                        "stars":	{"type":"short"},
                        "date":	{"type":"date"}
                    }
                }
            }
        }
    }
}
Add data
put /earth_index/_doc/1
{
    "title":"Nest eggs",
    "body":"Making your money work....",
    "comment":[
        {
        	"name":"JOhnSong",
            "comment":"Great article1",
           	"age":22,
            "stars":4,
            "date":"2019-11-13"
    	},
        {
        	"name":"superqiDream",
            "comment":"Great article2",
           	"age":23,
            "stars":5,
            "date":"2019-11-14"
    	}
    ]
}
Non-nested query
get /earth_index/_doc/_search
{
    "query":{
        "bool":{
            "must":[
                {"match":{"comment.name":"superqiDream"}},
                {"match":{"comment.age":23}}
            ]
        }
    }
}
POST /earth_index/_doc/_search
{
    "query":{
        "nested":{
            "path":"comment",
            "query":{
                "bool":{
                    "must":[
                        {
                            "match":{
                                "comment.name":"superqiDream"
                            }
                        },
                        {
                            "match":{
                                "comment.age":31
                            }
                        }
                    ]
                }
            }
        }
    }
}

New: POST

Modification: PUT

Delete: DELETE

Posted by Eugene on Tue, 19 Nov 2019 18:55:46 -0800