· 6 years ago · Jul 02, 2019, 07:46 AM
1To know:
2 Default port = 9200
3
4Concepts:
5 1. Cluster = Collection of nodes
6 2. Node = Instance of Elasticsearch
7 A Node is (pretty much) a server, virtual or physical
8 Each node contains a part of data
9 3. Index = Collection of documents and document properties
10 3.5 Shard = Index is divided into multiple shards
11 4. Type/Mapping = Collection of documents with common fields
12 They share an Index
13 5. Document:
14 A JSON object
15 Has a typeatomatom
16 Added to indeices
17
18Architecture:
19 Cluster:
20 Node
21 Node
22 Node
23
24Node:
25 Contains a part of the data
26
27
28
29
30Equivalents in a normal Database
31 Index : Database
32 Shard : Shard
33 Mapping : Table
34 Field : Field
35 JSON Object : Tuple
36
37
38
39A) Check Online:
40 GET ...
41B) Create Index (Database) named 'accounts':
42 PUT .../accounts
43 > {"acknowledged" : true}
44B2) More detailed
45 PUT .../accounts
46 < settings :
47 index :
48 number_of_shards : 3 // Default is 5
49 number_of_replicas : 2 // Default is 1
50B3) PUT .../accounts
51 < settings :
52 number_of_shards : 1
53 mappings :
54 type1 :
55 properties :
56 field1 : {type : "text"}
57
58
59C) Add to index 'accounts' a document of type 'person' with id '1'
60 POST .../accounts/person/1
61 < {
62 name : "Dave",
63 age : 21
64 }
65 > Response metadata
66D) Get document
67 GET .../accounts/person/1
68 >
69 _index : "accounts"
70 _type : "person"
71 _id : "1"
72 _version : 1
73 found : true
74 _source :
75 name : "Dave"
76 age : 21
77
78E) Update document
79 POST .../accounts/person/1/_update
80 <
81 doc :
82 name : "David Irimia"
83
84F) Search
85 GET .../_search?q=dave
86 >
87 metadata...
88 hits :
89 total : 1 // Number of document results
90 max_score : ?
91 hits : [
92 _index : "accounts",
93 ...
94 _score : 0.42874 // Nu stiu ce e asta
95 ...
96 _source :
97 name : "Dave",
98 age : 21
99 ]
100
101
102
103 GET .../_search?q=age:21
104 GET .../accounts/_search=q=age:21
105 GET .../accounts/person/_search=q=age:21
106 GET .../accounts/person,dog/_search=q=age:21 // In documente de mai multe tipuri
107 GET .../_all/person/_search?q=age:21
108
109
110
111F2) Search through request body
112 GET .../_search
113 <
114 query :
115 query_string :
116 default_field : "content" // Dont touch this
117 query : "john AND bob OR dave" // Search for this
118 default_operator : 'OR'
119
120 Ex: If default_operator is 'OR'
121 'capital of Hungary' => 'capital OR of OR Hungary'
122 (capital OR city) AND of AND Hungary // Paranthesis work
123 Query: same as if we typed it in the url
124 name:dave // The status field contains active
125 name:"dave irimia" // Searches exact
126 book.\*:(quick brown)
127 Gen book.*:quick brown // quick OR brown
128 Cauta book.title, book.date, book.content
129 _exists_:name // Where the name field exists
130
131 qu?ck brown // ? replaces any 1 character
132 quick bro* // * replaces 0 or more characters
133 qu?ck bro* // This works as expected
134 // THESE TAKE A LOT OF MEMORY
135 // VERY BAD PERFORMANCE
136
137 Fuzziness:
138 quikc~ brwn~ fxo~ // Maximum of 2 changes
139 quikc~1 // 1 Change, usually this covers 80% misspells
140 "fox quick"~5 // Proximity search. Should work as expected
141
142 Ranges: [min TO max]
143 age:[18 TO 23]
144 age:[10 TO *] // 10 and up
145 age:[>10] sau [<=10] sau
146 age:[* TO 23] // Until 23
147 age:{18 TO 23} // Excluding 18 and 23
148 age:{18 TO 23]
149 date:[2012-01-01 TO 2012-12-31]
150
151 Priority (Boosting):
152 quick^2 fox // If we are especially interested in quick foxes
153 // Default is 1, gen quick^1 fox^1 (quick fox)
154 quick^0.5 fox^2
155 "quick brown"^2 (fox man)^4
156
157 Operators:
158 capital AND of AND Hungary
159 quick -brown +fox
160 Can contain 'quick'
161 Must NOT contain 'brown'
162 Must contain 'fox'
163 quick AND NOT brown AND fox
164
165
166
167
168
169F3) Parameters:
170 1. default_operator = 'OR' or 'AND' (default is OR)
171 2. allow_leading_wildcard = true or false
172 If true, allows '*' or '?' as the first character
173 3. fields : ["content", "name"]
174 Searches these fields
175 4. split_on_whitespace = true or false (default is false)
176 "If true, "qui" will also return on "quick"
177 Otherwise, nah
178
179
180 Other weird params:
181 - analyzer
182 - enable_position_increments
183 - fuzzy_max_expansions
184 - fuzziness
185 - fuzzy_prefix_length
186
187F4) Query fields (query : {... match : ...)
188 1. match :
189 name : "Irimia"
190 // Searches all documents where name contains Irimia
191 match :
192
193 2. must : ????
194
195G) Deleting a document
196 DELETE .../accounts/person/1
197 DELETE .../accounts