Use kibana learning elasticsearch restful api (DSL)

Use kibana learning elasticsearch restful api (DSL)

1, understand the basic concepts elasticsearch
Index: Database
Type: the Table
the Document: Row
Filed: Field,

2, keyword:
the PUT index created, eg: PUT / movie_index new index movie_index
GET is used to retrieve data, EG: GET movie_index / Movie /. 1
the POST is used to modify the data, EG: movie_index the POST / Movie /. 3 / _Update
the DELETE used delete data

3, examples
below to demonstrate through the movie, a movie exists actor.
class Movie {public
String the above mentioned id;
// movie name
String name;
// watercress score
Double doubanScore;
// list of actors
List <the Actor> actorList;
}

the Actor class {public
String ID;
// actor name
String name;
}

3.1, adding an index
$ PUT / movie_index

3.2, delete the index
$ DELETE / movie_index

3.3 See all index Library
$ GET _cat / indices? V

3.4, the new document library index {new}
add three movies

PUT /movie_index/movie/1
{
"id":1,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"liu de hua"}
]
}

3.4、直接用id查找
$ GET movie_index/movie/1
$ GET movie_index/movie/2
$ GET /movie_index/movie/3

3.5, modify - overall replacement
and add no difference

PUT /movie_index/movie/3
{
"id":"3",
"name":"incident red sea",
"doubanScore":"5.0",
"actorList":[
{"id":"1","name":"zhang guo li 001"}
]
}

Can be re-run, _version been incremented.

3.6, modified - a field
the POST movie_index / Movie /. 3 / _Update
{
"DOC": {
"doubanScore": "7.0"
}
}

3.7, a delete the Document
DELETE movie_index / Movie / 3

3.8, all the search data type SELECT * from tname} {
the GET movie_index / Movie / _search
{
"Took":. 1, time consuming // ms
"timed_out": false, // whether a timeout
"_shards": {
"Total":. 5 // transmitted to all five fragments
"successful":. 5,
"Skipped": 0,
"failed": 0
},
"hits": {
"Total": 2, 2 // hit data
"max_score": 1, // maximum score
"hits": [// query results
{
"_index": "movie_index",
"_type": "Movie",
"_id": "2",
"_score": 1,
"_Source": {
"ID": 2,
"name": "operation meigong river",
"doubanScore": 8,
"actorList": [
{
"id": 3,
"name": "zhang han yu"
}
]
}
},
.....
]
}
}

3.9, query by the conditions (all)
the GET movie_index / Movie / _search
{
"Query": {
"MATCH_ALL": {}
}
}

3.10, according to the query word
{select * from tname where name like '% red%'}

GET movie_index/movie/_search
{
"query":{
"match": {"name":"red"}
}
}

3.11, sub-attribute query word by
the GET movie_index / Movie / _search
{
"Query": {
"match": { "actorList.name": "Zhang"}
}
}

3.12, fuzzy queries

Correction matching word when a word can not match exactly, es very close through an algorithm of words also give a certain score, you can check out, but consume more performance.
Movie_index the GET / Movie / _search
{
"Query": {
"Fuzzy": { "name": "RAD"}
}
}

It may be matched by the red recording rad, close matching data record.

3.13, Filter - filtered query
{select o * from (select * from tname where name like '% red%') o where o.actorList.id = 3.}

GET movie_index/movie/_search
{
"query":{
"match": {"name":"red"}
},
"post_filter":{
"term": {
"actorList.id": 3
}
}
}

3.14, filtration - before query filters (recommended)
in fact accurate, the query ES is divided into two kinds: Query (query) and filtration (filter). Query that is mentioned earlier query query it (inquiry) will calculate the default score for each document returned, then sorted according to their score. And filtration (filter) only selected data are consistent with and does not calculate a score, and it can be cached documents. So, just from performance considerations, faster than the query filter.

In other words, the filter for filtering data in a wide range, and is suitable for an exact match query data. When the general application, should use data filtering operation of the filter, then the data matching the query.

EG, the query comprising the performer ID 1 and 3, and the recording film comprises the name of red
{select * from tname where actorList.id in (1,3)}

Movie_index the GET / Movie / _search
{
"Query": {
"BOOL": {
"filter": [
{ "Term": { "actorList.id": ". 1"}},
{ "Term": { "actorList.id ":" 3 "}}
]
}
}
}
Note: filtering (filter) only selected data matching, the score is not calculated, the result is returned max_score field is always 0.

{select * from tname where actorList.id in (1,3) and name like '%red%'}
GET movie_index/movie/_search
{
"query": {
//通过bool进行组合查询
"bool": {
//过滤两个条件
"filter": [
{"term": {"actorList.id": "1"}},
{"term": {"actorList.id": "3"}}
],
"must": {
"match": {"name": "red"}
}
}
}
}

3.15, sorting
each database has sort:
Mysql, the Oracle, sqlserver default collation is ascending or descending it?
Mysql: Ascending

GET movie_index/movie/_search
{
"query":{
"match": {"name":"red sea"}
},
"sort": [
{
"doubanScore": {
"order": "desc"
}
}
]
}

3.16、分页查询
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"from": 0,
"size": 1
}

from: represents the beginning from the first few queries, the default starting from 0
Size: represents the number of data displayed per page

3.17, field specifies query
the GET movie_index / Movie / _search
{
"Query": { "MATCH_ALL": {}},
"_Source": [ "name", "doubanScore"]
}
Note: _source: hits following query result of _source

3.18、高亮
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red sea"}
},
"highlight": {
"fields": {"name":{} }
}
}

Edit custom label highlighting
the GET movie_index / Movie / _search
{
"Query": {
"match": { "name": "Red Sea"}
},
"highlight": {
"pre_tags": [ "<span>"] , // prefix tag
"post_tags": [ "</ span>"], // label suffix
"Fields": { "name": {}}
}
}

3.19, the polymerization
packets corresponding sql statement! group by!

Remove each actor starred in a total of how many movies
GET movie_index / Movie / _search
{
"AGGS": {
"groupby_actor": {
"Terms": {
"Field,": "actorList.name.keyword"
}
}
}
}
Note: groupby_actor polymerization alias corresponds to the variable, context reference


Each actor starred in the movie is the average number, and press Rating Sort by
GET movie_index / Movie / _search
{
"AGGS": {
"groupby_actor_id": {
"Terms": {
"Field,": "actorList.name.keyword",
"Order": {
"avg_score": "desc"
}
},
"AGGS": {
"avg_score": {
"AVG": {
"Field": "doubanScore"
}
}
}
}
}
}

4, on the mapping
said before type can be understood as a table, the data type for each field that is how to define it

Check to see mapping

Custom Type. {Type of custom fields in the table}
after their work is defined, the data type is not recommended deprecated given self-es

GET movie_index / _mapping / movie
virtually every type of field is what data type, defined by the mapping.

However, if the mapping system is not set automatically, according to a data format to the data format should be inferred.
to true / false boolean →
1020 → Long
20.1 → Double, float
"2018-02-01" → DATE
"the Hello world" → + keyword text
default text will only be word, keyword string is not the word.

In addition to automatic mapping definitions may also be defined manually, but only for the newly added, there is no data field are defined. Once you have modified the data can not do.

5, Chinese word
elasticsearch itself comes with Chinese word, it is simply to separate the Chinese word for word, there is no concept of vocabulary. But the actual application, users are based vocabulary conditions, match the query, if we can put words to paper as a unit separate from the cut, then the user can query a more appropriate match, the query speed more quickly.

Word download URL: https: //github.com/medcl/elasticsearch-analysis-ik/releases

https://www.cnblogs.com/linjiqin/p/10904876.html


5.1, install the Chinese word
downloaded zip package, unpacked into the directory under /home/es/elasticsearch-6.2.2/plugins/

Note: /home/es/elasticsearch-6.2.2/ installation directory is elasticsearch.

$ cd /home/es/elasticsearch-6.2.2/plugins/
$ unzip elasticsearch-analysis-ik-6.2.2.zip

The archive file is deleted! Otherwise failed to start!
$ Rm -rf elasticsearch-analysis-ik-6.2.2.zip

5.2, restart es, see plug-in is installed
$ sudo fuser -k -n tcp 9200
$ cd /home/es/elasticsearch-6.2.2/bin
$ ./elasticsearch &
$ $ curl HTTP: // localhost: 9200 / _cat / plugins
prMkj8M the Analysis 6.2.2-IK

5.3 test using
5.3.1, the default
GET movie_index / _analyze
{
"text": "I am Chinese"
}
aaa

5.3.2, using the word {is a simple way to word}
GET movie_index / _analyze
{
"Analyzer": "ik_smart",
"text": "I am Chinese"
}
bbb

5.3.3, another word is -ik_max_word
GET movie_index / _analyze
{
"Analyzer": "ik_max_word",
"text": "I am Chinese"
}
ccc
can see a different word, a word significantly different, so after the definition of a type no longer use the default mapping, and to manually created mapping, because the word is to be selected.

Guess you like

Origin www.cnblogs.com/linjiqin/p/11615350.html
Recommended