curl -XPOST 'http://localhost:9200/_bulk?pretty=true' -d '
{ "index": {"_index": "library", "_type": "book", "_id": "1"}}
{ "title": "All Quiet on the Western Front","otitle": "Im Westen nichts Neues","author": "Erich Maria Remarque","year": 1929,"characters": ["Paul Bäumer", "Albert Kropp", "Haie Westhus", "Fredrich Müller", "Stanislaus Katczinsky", "Tjaden"],"tags": ["novel"],"copies": 1, "available": true, "section" : 3}
{ "index": {"_index": "library", "_type": "book", "_id": "2"}}
{ "title": "Catch-22","author": "Joseph Heller","year": 1961,"characters": ["John Yossarian", "Captain Aardvark", "Chaplain Tappman", "Colonel Cathcart", "Doctor Daneeka"],"tags": ["novel"],"copies": 6, "available" : false, "section" : 1}
{ "index": {"_index": "library", "_type": "book", "_id": "3"}}
{ "title": "The Complete Sherlock Holmes","author": "Arthur Conan Doyle","year": 1936,"characters": ["Sherlock Holmes","Dr. Watson", "G. Lestrade"],"tags": [],"copies": 0, "available" : false, "section" : 12}
{ "index": {"_index": "library", "_type": "book", "_id": "4"}}
{ "title": "Crime and Punishment","otitle": "Преступлéние и наказáние","author": "Fyodor Dostoevsky","year": 1886,"characters": ["Raskolnikov", "Sofia Semyonovna Marmeladova"],"tags": [],"copies": 0, "available" : true}
'
curl -XGET 'http://localhost:9200/library/book/_search?pretty=true' -d '{
"query": {
"query_string": {
"query": "title:crime^10 +title:punishment -otitle:cat +author: (+Fyodor +dostoevsky)",
"default_field" : "title"
}
}
}'
//结果相同
curl -XGET 'http://localhost:9200/library/book/_search?pretty=true' -d '{
"query": {
"query_string": {
"query": "title:crime^10 +title:punishment -otitle:cat +author: (+Fyodor +dostoevsky)"
}
}
}'
解释: 1. 得到title字段中包含crime词条的文档,并且这些文档应该有10的加权。 2. 接下来文档title中包含punishment,而在otitle不包含cat。 3. 最后文档的author字段中包含Fyodor和dostoevsky关键词。
curl -XGET 'http://localhost:9200/library/book/_search?pretty=true' -d '{
"query": {
"query_string": {
"query": "crime punishment Neues",
"fields" : ["title", "otitle"],
"use_dis_max": true
}
}
}'
curl -XGET 'http://localhost:9200/library/book/_search?pretty=true' -d '{
"query": {
"multi_match": {
"query": "crime punishment Neues",
"fields" : ["title", "otitle"]
}
}
}'
simple_query_string查询同样支持Lucene查询语法;不同的是simple_query_string查询在解析查询query错误时不会抛出异常。
curl -XGET 'http://localhost:9200/library/book/_search?pretty=true' -d '{
"query": {
"simple_query_string": {
"query": "title:crime^10 +title:punishment-otitle:cat +author: (+Fyodor +dostoevsky)",
"default_operator" : "and"
}
}
}'