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