mapping json
http POST :9200/library
http PUT :9200/library/book/_mapping < data/book_mapping.json
bulk data
http POS :9200/_bulk < data/book_document.json
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}'
HTTP/1.1 200 OK
Content-Length: 446
Content-Type: application/json; charset=UTF-8
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "4",
"_index": "library",
"_score": 0.15342641,
"_source": {
"author": "Fyodor Dostoevsky",
"available": true,
"characters": [
"Raskolnikov",
"Sofia Semyonovna Marmeladova"
],
"copies": 0,
"otitle": "Преступлéние и наказáние",
"tags": [],
"title": "Crime and Punishment",
"year": 1886
},
"_type": "book"
}
],
"max_score": 0.15342641,
"total": 1
},
"timed_out": false,
"took": 2
}
from and size
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' from=20 size=9
HTTP/1.1 200 OK
Content-Length: 128
Content-Type: application/json; charset=UTF-8
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [],
"max_score": 0.15342641,
"total": 1
},
"timed_out": false,
"took": 1
}
version
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' version:='true'
min_score
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' min_score:='0.75'
fields
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' fields:='["title", "year"]'
partial_fields,有include和exclude两个属性
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' partial_fields:='{"partial1":{"include":["titl*"], "exclude":["chara*"]}}'
HTTP/1.1 200 OK
Content-Length: 250
Content-Type: application/json; charset=UTF-8
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "4",
"_index": "library",
"_score": 0.15342641,
"_type": "book",
"fields": {
"partial1": [
{
"title": "Crime and Punishment"
}
]
}
}
],
"max_score": 0.15342641,
"total": 1
},
"timed_out": false,
"took": 2
}
script_fields
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' script_fields:='{"correctYear":{"script":"_source.year - 1800"}}'
HTTP/1.1 200 OK
Content-Length: 223
Content-Type: application/json; charset=UTF-8
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "4",
"_index": "library",
"_score": 0.15342641,
"_type": "book",
"fields": {
"correctYear": [
86
]
}
}
],
"max_score": 0.15342641,
"total": 1
},
"timed_out": false,
"took": 4
}
给Script传递参数
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' script_fields:='{"correctYear":{"script":"_source.year - paramYear", "param":{"paramYear":1800}}}'
- query_then_fetch,默认的,首先在所有相关的shard上面执行语句,获取相关相关信息用以sort和rank documents,然后在从满足条件的shard里面查询数据。
- query_and_fetch,query在所有的shard上面执行并返回结果
- dfs_query_and_fetch
- dfs_query_then_fetch
- count,只是返回满足条件的文档数量
- scan
通过参数search_type指定
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime"}}' search_type==query_and_fetch
http GET :9200/library/book/_search query:='{"term":{"title":"crime"}}'
Term因为不可分割,只能是全匹配,并且不会被analyzed
http GET :9200/library/book/_search query:='{"terms":{"tags":["novel", "book"], "minimum_match":1}}'
minimal_match等于1,只需要一个term匹配就可以了,如果为2,就是两个都需要匹配
返回所有的documents
http GET :9200/library/book/_search query:='{"match_all":{}}'
http GET :9200/library/book/_search query:='{"match":{"title":"crime and punishment"}}'
- operator: and/or
- analyzer
- fuzziness
- prefix_length
- max_expansions
- zero_terms_query
- cutoff_frequency
http GET :9200/library/book/_search query:='{"match":{"title":{"query":"crime and punishment", "operator":"and"}}}'
- slop
- analyzer
http GET :9200/library/book/_search query:='{"match_phrase":{"title":{"query":"crime punishment", "slop":1}}}'
可以指定多个field进行查询
http GET :9200/library/book/_search query:='{"multi_match":{"query":"crime punishment","fields":["title", "otitle"]}}'
支持Lucene语法
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)", "default_field":"title"}}'
- query
- default_field
- default_operator
- analyzer
- allow_leading_wildcard
- lowercase_expand_terms
- enable_position_increments
- fuzzy_max_expansions
- fuzzy_prefix_length
- fuzzy_min_sim
- phrase_slop
- boost
- analyze_wildcard
- auto_generate_phrase_queries
- minimum_should_match
- lenient
mutli fields
http GET :9200/library/book/_search query:='{"query_string":{"query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)", "default_field":"title", "use_dis_max": true}}'
不同于query_string,对于错误query,不会抛出异常,只会忽略错误的部分
只针对id
http GET :9200/library/book/_search query:='{"ids":{"values":["1", "10"]}}'
限定document type
http GET :9200/library/book/_search query:='{"ids":{"values":["1", "10"], "type": "book"}}'
http GET :9200/library/book/_search query:='{"prefix":{"title":"cri"}}'
- gte
- gt
- lte
- lt
http GET :9200/library/book/_search query:='{"range":{"year":{"gte":1700, "lte":1900}}}'
- should
- must
- must_not
post_filter
http GET :9200/library/book/_search query:='{"match":{"title" : "Catch"}}' post_filter:='{"term":{"year":1961}}'
filtered
http GET :9200/library/book/_search query:='{"filtered":{"query":{"match":{"title": "Catch"}}, "filter":{"term":{"year":1961}}}}'
- range
- exists
- missing
- script
- type
- limit
- identifiers
http GET :9200/library/book/_search query:='{"term":{"title":"crime"}}' highlight:='{"fields":{"title":{}}}'
asc or desc
如果想让某个field排序,一定要在mapping里面指定,否则都会按照score来排序的