本文共 35074 字,大约阅读时间需要 116 分钟。
[looking@master ~]$ curl -X POST "localhost:9200/my_store/products/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }> { "index": { "_id": 2 }}> { "price" : 20, "productID" : "KDKE-B-9947-#kL5" }> { "index": { "_id": 3 }}> { "price" : 30, "productID" : "JODL-X-1937-#pV7" }> { "index": { "_id": 4 }}> { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }> '{ "took" : 1688, "errors" : false, "items" : [ { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 201 } } ]}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : { > "filter" : {> "term" : { > "price" : 20> }> }> }> }> }> '{ "took" : 275, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 1.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } } ] }}[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "term" : {> "price" : 20> }> }> }> '{ "took" : 16, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 1.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "term" : {> "productID" : "XHDK-A-1293-#fJ3"> }> }> }> }> }> '{ "took" : 75, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}[looking@master ~]$ curl -X GET "localhost:9200/my_store/_analyze?pretty" -H 'Content-Type: application/json' -d'> {> "field": "productID",> "text": "XHDK-A-1293-#fJ3"> }> '{ "tokens" : [ { "token" : "xhdk", "start_offset" : 0, "end_offset" : 4, "type" : "", "position" : 0 }, { "token" : "a", "start_offset" : 5, "end_offset" : 6, "type" : " ", "position" : 1 }, { "token" : "1293", "start_offset" : 7, "end_offset" : 11, "type" : " ", "position" : 2 }, { "token" : "fj3", "start_offset" : 13, "end_offset" : 16, "type" : " ", "position" : 3 } ]}
重新索引并设置 productID 字段为精确值(现在可以通过 productID 来进行查询了)。
[looking@master ~]$ curl -X PUT "localhost:9200/my_store?include_type_name=true&pretty" -H 'Content-Type: application/json' -d'> {> "mappings" : {> "products" : {> "properties" : {> "productID" : {> "type" : "keyword"> }> }> }> }> }> '{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "my_store"}[looking@master ~]$ curl -X POST "localhost:9200/my_store/products/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }> { "index": { "_id": 2 }}> { "price" : 20, "productID" : "KDKE-B-9947-#kL5" }> { "index": { "_id": 3 }}> { "price" : 30, "productID" : "JODL-X-1937-#pV7" }> { "index": { "_id": 4 }}> { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }> '{ "took" : 121, "errors" : false, "items" : [ { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_store", "_type" : "products", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 201 } } ]}[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "term" : {> "productID" : "XHDK-A-1293-#fJ3"> }> }> }> }> }> '{ "took" : 65, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "1", "_score" : 1.0, "_source" : { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "bool" : {> "filter" : {> "bool" : {> "should" : [> { "term" : {"price" : 20}},> { "term" : {"productID" : "XHDK-A-1293-#fJ3"}}> ],> "must_not" : {> "term" : {"price" : 30}> }> }> }> }> }> }> '{ "took" : 155, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "1", "_score" : 0.0, "_source" : { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" } }, { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 0.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "bool" : {> "filter" : {> "bool" : {> "should" : [> { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, > { "bool" : { > "must" : [> { "term" : {"productID" : "JODL-X-1937-#pV7"}}, > { "term" : {"price" : 30}} > ]> }}> ]> }> }> }> }> }> '{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 0.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } }, { "_index" : "my_store", "_type" : "products", "_id" : "3", "_score" : 0.0, "_source" : { "price" : 30, "productID" : "JODL-X-1937-#pV7" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "terms" : { > "price" : [20, 30]> }> }> }> }> }> '{ "took" : 14, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 1.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } }, { "_index" : "my_store", "_type" : "products", "_id" : "3", "_score" : 1.0, "_source" : { "price" : 30, "productID" : "JODL-X-1937-#pV7" } }, { "_index" : "my_store", "_type" : "products", "_id" : "4", "_score" : 1.0, "_source" : { "price" : 30, "productID" : "QQPX-R-3956-#aD8" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "range" : {> "price" : {> "gte" : 20,> "lt" : 40> }> }> }> }> }> }> '{ "took" : 79, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "2", "_score" : 1.0, "_source" : { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } }, { "_index" : "my_store", "_type" : "products", "_id" : "3", "_score" : 1.0, "_source" : { "price" : 30, "productID" : "JODL-X-1937-#pV7" } }, { "_index" : "my_store", "_type" : "products", "_id" : "4", "_score" : 1.0, "_source" : { "price" : 30, "productID" : "QQPX-R-3956-#aD8" } } ] }}
# 假设有 timestamp 字段的话可以这么写,不过这儿并没有curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{ "query" : { "constant_score" : { "filter" : { "range" : { "timestamp" : { "gt" : "2014-01-01 00:00:00", "lt" : "2014-01-07 00:00:00" } } } } }}'curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{ "query" : { "constant_score" : { "filter" : { "range" : { "timestamp" : { "gt" : "now-1h" } } } } }}'curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{ "query" : { "constant_score" : { "filter" : { "range" : { "timestamp" : { "gt" : "2014-01-01 00:00:00", "lt" : "2014-01-01 00:00:00||+1M" } } } } }}'
[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "range" : {> "productID" : {> "gte" : "O",> "lt" : "Z"> }> }> }> }> }> }> '{ "took" : 357, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_store", "_type" : "products", "_id" : "1", "_score" : 1.0, "_source" : { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" } }, { "_index" : "my_store", "_type" : "products", "_id" : "4", "_score" : 1.0, "_source" : { "price" : 30, "productID" : "QQPX-R-3956-#aD8" } } ] }}
curl -X POST "localhost:9200/my_index/posts/_bulk?pretty" -H 'Content-Type: application/json' -d'{ "index": { "_id": "1" }}{ "tags" : ["search"] }{ "index": { "_id": "2" }}{ "tags" : ["search", "open_source"] }{ "index": { "_id": "3" }}{ "other_field" : "some data" }{ "index": { "_id": "4" }}{ "tags" : null }{ "index": { "_id": "5" }}{ "tags" : ["search", null] }'{ "took" : 288, "errors" : false, "items" : [ { "index" : { "_index" : "my_index", "_type" : "posts", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, ... { "index" : { "_index" : "my_index", "_type" : "posts", "_id" : "5", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1, "status" : 201 } } ]}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/posts/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "constant_score" : {> "filter" : {> "exists" : { "field" : "tags" }> }> }> }> }> '{ "took" : 46, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_index", "_type" : "posts", "_id" : "1", "_score" : 1.0, "_source" : { "tags" : [ "search" ] } }, { "_index" : "my_index", "_type" : "posts", "_id" : "2", "_score" : 1.0, "_source" : { "tags" : [ "search", "open_source" ] } }, { "_index" : "my_index", "_type" : "posts", "_id" : "5", "_score" : 1.0, "_source" : { "tags" : [ "search", null ] } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/posts/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query" : {> "bool" : {> "must_not" : {> "exists" : { "field" : "tags" }> }> }> }> }> '{ "took" : 28, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.0, "hits" : [ { "_index" : "my_index", "_type" : "posts", "_id" : "3", "_score" : 0.0, "_source" : { "other_field" : "some data" } }, { "_index" : "my_index", "_type" : "posts", "_id" : "4", "_score" : 0.0, "_source" : { "tags" : null } } ] }}
[looking@master ~]$ curl -X DELETE "localhost:9200/my_index?pretty"{ "acknowledged" : true}[looking@master ~]$ curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'> { "settings": { "number_of_shards": 1 }}> '{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "my_index"}[looking@master ~]$ curl -X POST "localhost:9200/my_index/my_type/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "title": "The quick brown fox" }> { "index": { "_id": 2 }}> { "title": "The quick brown fox jumps over the lazy dog" }> { "index": { "_id": 3 }}> { "title": "The quick brown fox jumps over the quick dog" }> { "index": { "_id": 4 }}> { "title": "Brown fox brown dog" }> '{ "took" : 73, "errors" : false, "items" : [ { "index" : { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 201 } } ]}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "match": {> "title": "QUICK!"> }> }> }> '{ "took" : 76, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 0.44255546, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.44255546, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.42327404, "_source" : { "title" : "The quick brown fox" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.30818442, "_source" : { "title" : "The quick brown fox jumps over the lazy dog" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "match": {> "title": "BROWN DOG!"> }> }> }> '{ "took" : 75, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.58571666, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_score" : 0.58571666, "_source" : { "title" : "Brown fox brown dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.399221, "_source" : { "title" : "The quick brown fox jumps over the lazy dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.399221, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.12503365, "_source" : { "title" : "The quick brown fox" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "match": {> "title": { > "query": "BROWN DOG!",> "operator": "and"> }> }> }> }> '{ "took" : 27, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 0.58571666, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_score" : 0.58571666, "_source" : { "title" : "Brown fox brown dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.399221, "_source" : { "title" : "The quick brown fox jumps over the lazy dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.399221, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "match": {> "title": {> "query": "quick brown dog",> "minimum_should_match": "75%"> }> }> }> }> '{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.84177643, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.84177643, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.7074054, "_source" : { "title" : "The quick brown fox jumps over the lazy dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_score" : 0.58571666, "_source" : { "title" : "Brown fox brown dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.54830766, "_source" : { "title" : "The quick brown fox" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "must": { "match": { "title": "quick" }},> "must_not": { "match": { "title": "lazy" }},> "should": [> { "match": { "title": "brown" }},> { "match": { "title": "dog" }}> ]> }> }> }> '{ "took" : 239, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.8417765, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.8417765, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.54830766, "_source" : { "title" : "The quick brown fox" } } ] }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { "title": "brown" }},> { "match": { "title": "fox" }},> { "match": { "title": "dog" }}> ],> "minimum_should_match": 2 > }> }> }> '{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 0.71075034, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_score" : 0.71075034, "_source" : { "title" : "Brown fox brown dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.49025756, "_source" : { "title" : "The quick brown fox jumps over the lazy dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.49025756, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.2500673, "_source" : { "title" : "The quick brown fox" } } ] }}
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "should": [ { "match": { "title": "brown" }}, { "match": { "title": "fox" }} ] } }}'curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "should": [ { "match": { "title": "brown fox" }} ] } }}'
[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "must": {> "match": {> "content": { > "query": "full text search",> "operator": "and"> }> }> },> "should": [ > { "match": { "content": "Elasticsearch" }},> { "match": { "content": "Lucene" }}> ]> }> }> }> '{ "took" : 64, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "must": {> "match": { > "content": {> "query": "full text search",> "operator": "and"> }> }> },> "should": [> { "match": {> "content": {> "query": "Elasticsearch",> "boost": 3 > }> }},> { "match": {> "content": {> "query": "Lucene",> "boost": 2 > }> }}> ]> }> }> }> '{ "took" : 6, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}
新增字段
[looking@master ~]$ curl -X PUT "localhost:9200/my_index/_mapping?pretty" -H 'Content-Type: application/json' -d'> {> "properties": {> "english_title": {> "type": "text",> "analyzer": "english"> }> }> }> '{ "acknowledged" : true}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/_analyze?pretty" -H 'Content-Type: application/json' -d'> {> "field": "my_type.title", > "text": "Foxes"> }> '{ "tokens" : [ { "token" : "foxes", "start_offset" : 0, "end_offset" : 5, "type" : "", "position" : 0 } ]}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_validate/query?explain&pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { "title": "Foxes"}},> { "match": { "english_title": "Foxes"}}> ]> }> }> }> '{ "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "valid" : true, "explanations" : [ { "index" : "my_index", "valid" : true, "explanation" : "+(title:foxes english_title:fox) #*:*" } ]}
[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { "title": "War and Peace" }},> { "match": { "author": "Leo Tolstoy" }}> ]> }> }> }> '{ "took" : 267, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { "title": "War and Peace" }},> { "match": { "author": "Leo Tolstoy" }},> { "bool": {> "should": [> { "match": { "translator": "Constance Garnett" }},> { "match": { "translator": "Louise Maude" }}> ]> }}> ]> }> }> }> '{ "took" : 10, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { > "title": {> "query": "War and Peace",> "boost": 2> }}},> { "match": { > "author": {> "query": "Leo Tolstoy",> "boost": 2> }}},> { "bool": { > "should": [> { "match": { "translator": "Constance Garnett" }},> { "match": { "translator": "Louise Maude" }}> ]> }}> ]> }> }> }> '{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }}
[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {> "query": {> "bool": {> "should": [> { "match": { "title": "Brown fox" }},> { "match": { "body": "Brown fox" }}> ]> }> }> }> '{ "took" : 535, "timed_out" : false, "_shards" : { "total" : 9, "successful" : 9, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.6611805, "hits" : [ { "_index" : "my_index", "_type" : "my_type", "_id" : "4", "_score" : 1.6611805, "_source" : { "title" : "Brown fox brown dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "3", "_score" : 0.9979023, "_source" : { "title" : "The quick brown fox jumps over the quick dog" } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "2", "_score" : 0.7026868, "_source" : { "title" : "Keeping pets healthy", "body" : "My quick brown fox eats rabbits on a regular basis." } }, { "_index" : "my_index", "_type" : "my_type", "_id" : "1", "_score" : 0.62098616, "_source" : { "title" : "Quick brown rabbits", "body" : "Brown rabbits are commonly seen." } } ] }}
转载地址:http://bjjqi.baihongyu.com/