项目作者: ameizi

项目描述 :
ElasticSearch Java Rest Client Examples
高级语言: Java
项目地址: git://github.com/ameizi/elasticsearch-jest-example.git
创建时间: 2014-11-20T16:12:32Z
项目社区:https://github.com/ameizi/elasticsearch-jest-example

开源协议:MIT License

下载


jest

ElasticSearch Java Rest Client Examples

高亮查询(highlight)

  1. POST http://127.0.0.1:9200/news/_search?q=李克强
  2. {
  3. "query" : {
  4. match_all:{}
  5. },
  6. "highlight" : {
  7. "pre_tags" : ["<font color='red'>", "<b>", "<em>"],
  8. "post_tags" : ["</font>", "<b>", "</em>"],
  9. "fields" : [
  10. {"title" : {}},
  11. {"content" : {
  12. "fragment_size" : 350,
  13. "number_of_fragments" : 3,
  14. "no_match_size": 150
  15. }}
  16. ]
  17. }
  18. }
  1. POST http://127.0.0.1:9200/news/_search?q=李克强
  2. {
  3. "query" : {
  4. match_all:{}
  5. },
  6. "highlight" : {
  7. "pre_tags" : ["<font color='red'><b><em>"],
  8. "post_tags" : ["</font><b></em>"],
  9. "fields" : [
  10. {"title" : {}},
  11. {"content" : {
  12. "fragment_size" : 350,
  13. "number_of_fragments" : 3,
  14. "no_match_size": 150
  15. }}
  16. ]
  17. }
  18. }

删除索引

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html

  1. DELETE http://127.0.0.1:9200/news

创建索引

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

  1. PUT http://127.0.0.1:9200/news

创建或修改mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

  1. PUT /{index}/_mapping/{type}
  1. PUT http://127.0.0.1:9200/news/_mapping/article
  2. {
  3. "article": {
  4. "properties": {
  5. "pubdate": {
  6. "type": "date",
  7. "format": "dateOptionalTime"
  8. },
  9. "author": {
  10. "type": "string"
  11. },
  12. "content": {
  13. "type": "string"
  14. },
  15. "id": {
  16. "type": "long"
  17. },
  18. "source": {
  19. "type": "string"
  20. },
  21. "title": {
  22. "type": "string"
  23. },
  24. "url": {
  25. "type": "string"
  26. }
  27. }
  28. }
  29. }

查看mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

  1. GET http://127.0.0.1:9200/_all/_mapping
  2. GET http://127.0.0.1:9200/_mapping
  1. GET http://127.0.0.1:9200/news/_mapping/article

输出:

  1. {
  2. "news": {
  3. "mappings": {
  4. "article": {
  5. "properties": {
  6. "author": {
  7. "type": "string"
  8. },
  9. "content": {
  10. "type": "string"
  11. },
  12. "id": {
  13. "type": "long"
  14. },
  15. "pubdate": {
  16. "type": "date",
  17. "store": true,
  18. "format": "yyyy-MM-dd HH:mm:ss"
  19. },
  20. "source": {
  21. "type": "string"
  22. },
  23. "title": {
  24. "type": "string"
  25. },
  26. "url": {
  27. "type": "string"
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }

删除mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-mapping.html

  1. [DELETE] /{index}/{type}
  2. [DELETE] /{index}/{type}/_mapping
  3. [DELETE] /{index}/_mapping/{type}
  1. DELETE http://127.0.0.1:9200/news/_mapping/article

ansj分词器测试

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=习近平

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=我是中国人

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=汪东兴同志遗体在京火化汪东兴同志病重期间和逝世后,习近平李克强张德江俞正声刘云山王岐山张高丽江泽民胡锦涛等同志,前往医院看望或通过各种形式对汪东兴同志逝世表示沉痛哀悼并向其亲属表示深切慰问新华社北京8月27日电中国共产党的优秀党员

ansj分词器查询

  • 普通查询

http://127.0.0.1:9200/news/_search?q=习近平&analyzer=ansj_index&size=50

  • 指定term查询

http://127.0.0.1:9200/news/_search?q=content:江泽民&analyzer=ansj_index&size=50

http://127.0.0.1:9200/news/_search?q=title:江泽民&analyzer=ansj_index&size=50

http://127.0.0.1:9200/news/_search?q=source:新华网&analyzer=ansj_index&size=50

  • 其中ansj_index为在elasticsearch.yml文件中配置的ansj分词器

elasticsearch rest api 快速上手

elasticsearch-jdbc

  1. @echo off
  2. set DIR=%~dp0
  3. set LIB="%DIR%\..\lib\*"
  4. set BIN="%DIR%\..\bin\*"
  5. REM ???
  6. echo {^
  7. "type" : "jdbc",^
  8. "jdbc" : {^
  9. "url" : "jdbc:mysql://localhost:3306/news",^
  10. "user" : "root",^
  11. "password" : "root",^
  12. "schedule" : "0 0/15 * ? * *",^
  13. "sql" : [^
  14. {"statement":"SELECT title,content,url,source,author,pubdate FROM news"},^
  15. {^
  16. "statement":"SELECT title,content,url,source,author,pubdate FROM news where pubdate > ?",^
  17. "parameter" : [ "$metrics.lastexecutionstart" ]^
  18. }^
  19. ],^
  20. "autocommit" : true,^
  21. "treat_binary_as_string" : true,^
  22. "elasticsearch" : {^
  23. "cluster" : "elasticsearch",^
  24. "host" : "localhost",^
  25. "port" : 9300^
  26. },^
  27. "index" : "news",^
  28. "type" : "article"^
  29. }^
  30. }^ | "%JAVA_HOME%\bin\java" -cp "%LIB%" -Dlog4j.configurationFile="file://%DIR%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"

elasticsearch-jdbc 插件的使用