极简设计

elasticsearch

http://localhost:9200/?pretty

关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns) Elasticsearch ⇒ 索引 ⇒ 类型 ⇒ 文档 ⇒ 字段(Fields)

chrome postman中 PUT http://127.0.0.1:9200/megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] }

http://127.0.0.1:9200/megacorp/employee/2 { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } http://127.0.0.1:9200/megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] }

浏览 http://127.0.0.1:9200/megacorp/employee/1

搜索全部,默认返回最前10 http://127.0.0.1:9200/megacorp/employee/_search

查询字符串搜索 http://127.0.0.1:9200/megacorp/employee/_search?q=last_name:Smith

dsl搜索,postman中 http://127.0.0.1:9200/megacorp/employee/_search

{ "query" : { "match" : { "last_name" : "Smith" } } }

http://127.0.0.1:9200/megacorp/employee/_search { "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "Smith" } } } } }

全文搜索,所有喜欢rock climbing 的员工 http://127.0.0.1:9200/megacorp/employee/_search { "query" : { "match" : { "about" : "rock climbing" } } } 返回2条数据 ,根据相关性计算,与传统 DB的最大区别

{ "query" : { "match_phrase" : { "about" : "rock climbing" } } } about 字段只包含 rock climbing 的短语

汇总 (aggregations),进行复杂统计。类似 SQL GROUP BY,更加强大

找员工中最受欢迎的兴趣是什么: { "aggs": { "all_interests": { "terms": { "field": "interests" } } } }

加条件 { "query": { "match": { "last_name": "smith" } }, "aggs": { "all_interests": { "terms": { "field": "interests" } } } }

文档元数据

index/type/_id 必须有

curl -XPUT 'http://localhost:9200/us/user/1?pretty=1' -d ' { "email" : "john@smith.com", "name" : "John Smith", "username" : "@john" } '

curl -XPUT 'http://localhost:9200/gb/user/2?pretty=1' -d ' { "email" : "mary@jones.com", "name" : "Mary Jones", "username" : "@mary" } '

curl -XPUT 'http://localhost:9200/gb/tweet/3?pretty=1' -d ' { "date" : "2014-09-13", "name" : "Mary Jones", "tweet" : "Elasticsearch means full text search has never been so easy", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/4?pretty=1' -d ' { "date" : "2014-09-14", "name" : "John Smith", "tweet" : "@mary it is not just text, it does everything", "user_id" : 1 } '

curl -XPUT 'http://localhost:9200/gb/tweet/5?pretty=1' -d ' { "date" : "2014-09-15", "name" : "Mary Jones", "tweet" : "However did I manage before Elasticsearch?", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/6?pretty=1' -d ' { "date" : "2014-09-16", "name" : "John Smith", "tweet" : "The Elasticsearch API is really easy to use", "user_id" : 1 } '

curl -XPUT 'http://localhost:9200/gb/tweet/7?pretty=1' -d ' { "date" : "2014-09-17", "name" : "Mary Jones", "tweet" : "The Query DSL is really powerful and flexible", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/8?pretty=1' -d ' { "date" : "2014-09-18", "name" : "John Smith", "user_id" : 1 } '

curl -XPUT 'http://localhost:9200/gb/tweet/9?pretty=1' -d ' { "date" : "2014-09-19", "name" : "Mary Jones", "tweet" : "Geo-location aggregations are really cool", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/10?pretty=1' -d ' { "date" : "2014-09-20", "name" : "John Smith", "tweet" : "Elasticsearch surely is one of the hottest new NoSQL products", "user_id" : 1 } '

curl -XPUT 'http://localhost:9200/gb/tweet/11?pretty=1' -d ' { "date" : "2014-09-21", "name" : "Mary Jones", "tweet" : "Elasticsearch is built for the cloud, easy to scale", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/12?pretty=1' -d ' { "date" : "2014-09-22", "name" : "John Smith", "tweet" : "Elasticsearch and I have left the honeymoon stage, and I still love her.", "user_id" : 1 } '

curl -XPUT 'http://localhost:9200/gb/tweet/13?pretty=1' -d ' { "date" : "2014-09-23", "name" : "Mary Jones", "tweet" : "So yes, I am an Elasticsearch fanboy", "user_id" : 2 } '

curl -XPUT 'http://localhost:9200/us/tweet/14?pretty=1' -d ' { "date" : "2014-09-24", "name" : "John Smith", "tweet" : "How many more cheesy tweets do I have to write?", "user_id" : 1 } '

ES_HEAP_SIZE设小点,留50%内存给Lucene使用?

Elasticsearch内置分词器(analyzers):standard/english/chinese standard无脑的一个一个词(汉字)切分,适用范围广,但是精准度低 english可以识别单数负数,大小写,过滤stopwords(例如“the”这个词)等 chinese 效果很差

https://github.com/medcl/elasticsearch-analysis-ik

wget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip unzip master.zip cd elasticsearch-analysis-ik/ mvn clean package -Dmaven.test.skip

elasticsearch-analysis-ik\target\releases\elasticsearch-analysis-ik-1.7.0.zip 解压到 plugins\ik

elasticsearch.yml 最后加 index.analysis.analyzer.ik.type : "ik"

http://127.0.0.1:9200/analyze?analyzer=ik http://127.0.0.1:9200/analyze?analyzer=ik_smart

测试语句 为Elasticsearch添加中文分词,对比分词器效果 联想召回笔记本电源线

http://blog.mkfree.com/posts/40 https://www.gitbook.com/book/giraffe0813/spring-data-elasticsearch/details

in java with : java