1、准備數據
PUT /myindex/article/1 { "post_date":"2018-05-10", "title":"Java", "content":"java is the best language", "author_id":119 } PUT /myindex/article/2 { "post_date":"2018-05-12", "title":"html", "content":"I like html", "author_id":120 } PUT /myindex/article/3 { "post_date":"2018-05-16", "title":"es", "content":"Es is distributed document store", "author_id":110 }
2、操作演示
1)查詢myindex索引下所有文檔
GET /myindex/article/_search
查詢結果
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "myindex", "_type": "article", "_id": "2", "_score": 1, "_source": { "post_date": "2018-05-12", "title": "html", "content": "I like html", "author_id": 120 } }, { "_index": "myindex", "_type": "article", "_id": "1", "_score": 1, "_source": { "post_date": "2018-05-10", "title": "Java", "content": "java is the best language", "author_id": 119 } }, { "_index": "myindex", "_type": "article", "_id": "3", "_score": 1, "_source": { "post_date": "2018-05-16", "title": "es", "content": "Es is distributed document store", "author_id": 110 } } ] } }
2)查詢myindex索引下日期是2018-05-10的文檔。日期類型不會分詞,要精確查詢
GET /myindex/article/_search?q=post_date:2018-05-10
查詢結果
{ "took": 31, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "myindex", "_type": "article", "_id": "1", "_score": 1, "_source": { "post_date": "2018-05-10", "title": "Java", "content": "java is the best language", "author_id": 119 } } ] } }
3)查詢myindex索引下content字段中含有html的文檔
GET /myindex/article/_search?q=content:html
查詢結果
{ "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "myindex", "_type": "article", "_id": "2", "_score": 0.2876821, "_source": { "post_date": "2018-05-12", "title": "html", "content": "I like html", "author_id": 120 } } ] } }
4)查詢myindex索引下,字段中含有html或者document的文檔,不指定字段,所有字段都會對比,性能低
GET /myindex/article/_search?q=html,document
查詢結果
{ "took": 19, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "myindex", "_type": "article", "_id": "2", "_score": 0.2876821, "_source": { "post_date": "2018-05-12", "title": "html", "content": "I like html", "author_id": 120 } }, { "_index": "myindex", "_type": "article", "_id": "3", "_score": 0.2876821, "_source": { "post_date": "2018-05-16", "title": "es", "content": "Es is distributed document store", "author_id": 110 } } ] } }
3、使用copy_to字段解決低性能問題
1)該字段是把其他字段中的值,以空格為分隔符組成一個大字符串,然后被分析和索引但是不存儲,也就是說它能被查詢,但是不能被取回顯示。
2)注意copy_to指向的字段,字段類型要為text
3)當沒有指定查詢的字段時,就會從copy_to字段中查詢
需要自己創建mapping,不能用默認的,下面創建mapping,先要刪除掉之前的
DELETE myindex
PUT /myindex PUT /myindex/article/_mapping { "properties":{ "post_date":{ "type":"date" }, "title":{ "type":"text", "copy_to":"fullcontents" }, "content":{ "type":"text", "copy_to":"fullcontents" }, "author_id":{ "type":"integer" } } }
准備數據,還用前面的
PUT /myindex/article/1 { "post_date":"2018-05-10", "title":"Java", "content":"java is the best language", "author_id":119 } PUT /myindex/article/2 { "post_date":"2018-05-12", "title":"html", "content":"I like html", "author_id":120 } PUT /myindex/article/3 { "post_date":"2018-05-16", "title":"es", "content":"Es is distributed document store", "author_id":110 }
查詢:myindex索引下含有html、document的文檔
GET /myindex/article/_search?q=fullcontents:html,document
查詢結果
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.39556286, "hits": [ { "_index": "myindex", "_type": "article", "_id": "2", "_score": 0.39556286, "_source": { "post_date": "2018-05-12", "title": "html", "content": "I like html", "author_id": 120 } }, { "_index": "myindex", "_type": "article", "_id": "3", "_score": 0.2876821, "_source": { "post_date": "2018-05-16", "title": "es", "content": "Es is distributed document store", "author_id": 110 } } ] } }