Elasticsearch笔记

资料

官网: http://www.elasticsearch.org

中文资料:http://www.learnes.net/

.Net驱动: http://nest.azurewebsites.net/

 

教程: http://www.jianshu.com/p/05cff717563c

 

三级: 索引/类型/ID或操作符

对应: 数据库/表/ID

配置

启动服务: /es/elasticsearch -d 

添加认证: http://segmentfault.com/a/1190000002803609

用户名: admin , admin_pw

 

关闭: curl -XPOST http://localhost:9200/_cluster/nodes/_shutdown

关闭某一个索引: curl -XPOST http://localhost:9200/_cluster/nodes/[某一个索引]/_shutdown

查看索引: curl -XGet http://localhost:9200/_cluster/nodes

插件

Head

elasticsearch/bin/plugin -install mobz/elasticsearch-head

http://localhost:9200/_plugin/head/

分词

IK官网:https://github.com/medcl/elasticsearch-analysis-ik

安装IK:bin/plugin -install medcl/elasticsearch-analysis-ik  

 

安装分词需要 Jdk7,Maven,Git,在最新的ES(1.7.1)上,网上的文章大部分都是错的。正确方法: 

http://my.oschina.net/xiaohui249/blog/232784

http://www.360doc.com/content/15/0114/13/16070877_440673208.shtml

如果这两篇文章存在有冲突的地方,使用后者。

步骤:

1.安装Jdk7,然后,再运行: set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_80\bin

注意:安装的Jdk版本一定要和服务器使用的JRE版本一致。否则ES启动后,不可使用。

2.安装 Maven,Git

3.进入 D:\ik ,运行: clone https://github.com/medcl/elasticsearch-analysis-ik

4.进入 D:\ik\elasticsearch-analysis-ik

5.运行:mvn clean package

6.生成的 jar 在 D:\ik\elasticsearch-analysis-ik\target\releases\elasticsearch-analysis-ik-1.4.0.zip , 其它的 jar 全是扯蛋。把它解出来。

附上链接: 百度网盘下载:elasticsearch-analysis-ik-1.4.0.zip

7. 把 解压缩的jar 拷贝到: ES_HOME/plugins/analysis-ik

8. 将下载目录中的ik目录拷贝到ES_HOME/config目录下面。

9.打开ES_HOME/config/elasticsearch.yml文件,在文件最后加入如下内容

index:
  analysis:                   
    analyzer:      
      ik:
          alias: [ik_analyzer]
          type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
          type: ik
          use_smart: false
      ik_smart:
          type: ik
          use_smart: true

或:

index.analysis.analyzer.default.type: ik

10. 重新启动ES

11. 建一个索引,测试: 

 

curl -XPut 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d '
{
"text":"世界如此之大"
}'

 

 

Maven

安装教程:http://jingyan.baidu.com/article/1709ad808ad49f4634c4f00d.html

官网:http://maven.apache.org/download.cgi

Git

下载:http://www.git-scm.com/download/win

过程

1. 创建索引:  Put ~/index

2. 获取所有Index: Get ~/_aliases

3. 创建别名: Put ~/<Index>/_alias/<Index_Alias>

4. 创建模板: Put  ~/<index>/_mapping/<type>  {type: {properties: { "列": { type:"string" }, "列2" : { type:  { Id: { type: "int"} , Name: { type: "string"} } } }}} 

对于《列2》来说,是一个对象类型,它可以是对象,也可以是对象数组。

更复杂的实例:

{
  "hr": {
    "properties": {
      "ResumeName": {
        "type": "string"
      },
      "Source": {
        "type": "integer"
      },
      "PersonInfo": {
        "properties": {
          "Name": {
            "type": "string",
            "analyzer": "ik",
            "include_in_all": true
          },
          "Sex": {
            "type": "integer",
            "analyzer": "ik",
            "include_in_all": true
          },
          "BirthDay": {
            "type": "date",
            "analyzer": "ik",
            "include_in_all": true
          },
          "Mobile": {
            "type": "string"
          },
          "StartWorkAt": {
            "type": "date",
            "analyzer": "ik",
            "include_in_all": true
          },
          "LiveCity": {
            "type": "string",
            "analyzer": "ik",
            "include_in_all": true
          },
          "JobWantedStatus": {
            "type": "integer",
            "analyzer": "ik",
            "include_in_all": true
          },
          "NativePlace": {
            "type": "string"
          },
          "MonthlySalary": {
            "type": "integer"
          },
          "YearSalary": {
            "type": "integer"
          },
          "National": {
            "type": "integer"
          },
          "IDCarNumber": {
            "type": "string"
          },
          "Height": {
            "type": "integer"
          },
          "MaritalStatus": {
            "type": "integer"
          },
          "WxNumber": {
            "type": "string"
          },
          "QqNumber": {
            "type": "integer"
          },
          "LiveAddress": {
            "type": "string"
          },
          "HomePage": {
            "type": "string"
          }
        }
      },
      "Intention": {
        "properties": {
          "Keyword": {
            "type": "string"
          },
          "SelfEvaluation": {
            "type": "string"
          },
          "ExpectWorkIn": {
            "type": "string",
            "analyzer": "ik",
            "include_in_all": true
          },
          "IndustryID": {
            "type": "integer"
          },
          "IndustryName": {
            "type": "string"
          },
          "PositionID": {
            "type": "integer"
          },
          "PositionName": {
            "type": "string"
          },
          "Nature": {
            "type": "integer"
          },
          "ExpectSalary": {
            "type": "integer",
            "analyzer": "ik",
            "include_in_all": true
          },
          "ArriveAt": {
            "type": "date"
          }
        }
      },
      "Education": {
        "properties": {
          "StartAt": {
            "type": "date"
          },
          "EndAt": {
            "type": "date"
          },
          "School": {
            "type": "string"
          },
          "Major": {
            "type": "string",
            "analyzer": "ik",
            "include_in_all": true
          },
          "Education": {
            "type": "integer",
            "analyzer": "ik",
            "include_in_all": true
          },
          "Remark": {
            "type": "string"
          },
          "IsForeign": {
            "type": "boolean"
          }
        }
      },
      "Experience": {
        "properties": {
          "StartAt": {
            "type": "date"
          },
          "EndAt": {
            "type": "date"
          },
          "Corporation": {
            "type": "string"
          },
          "IndustryID": {
            "type": "integer"
          },
          "IndustryName": {
            "type": "string"
          },
          "Size": {
            "type": "integer"
          },
          "Nature": {
            "type": "integer"
          },
          "PositionID": {
            "type": "integer"
          },
          "PositionName": {
            "type": "string"
          },
          "Remark": {
            "type": "string"
          },
          "WorkTimeType": {
            "type": "integer"
          }
        }
      },
      "Train": {
        "properties": {
          "StartAt": {
            "type": "date"
          },
          "EndAt": {
            "type": "date"
          },
          "TrainingInstitution": {
            "type": "string"
          },
          "TrainingPlace": {
            "type": "string"
          },
          "TrainingCourse": {
            "type": "string"
          },
          "Certificate": {
            "type": "string"
          },
          "Remark": {
            "type": "string"
          }
        }
      },
      "Language": {
        "properties": {
          "LanguageType": {
            "type": "integer"
          },
          "Degree": {
            "type": "integer"
          },
          "Level": {
            "type": "string"
          }
        }
      },
      "AdditionalInfo": {
        "properties": {
          "Subject": {
            "type": "string"
          },
          "Content": {
            "type": "string"
          }
        }
      },
      "Certificate": {
        "properties": {
          "Name": {
            "type": "string"
          },
          "GetAt": {
            "type": "date"
          },
          "Score": {
            "type": "string"
          }
        }
      },
      "Project": {
        "properties": {
          "StartAt": {
            "type": "date"
          },
          "EndAt": {
            "type": "date"
          },
          "Name": {
            "type": "string",
            "analyzer": "ik",
            "include_in_all": true
          },
          "Development": {
            "type": "string"
          },
          "Remark": {
            "type": "string"
          },
          "MyDuty": {
            "type": "string"
          }
        }
      },
      "ItSkill": {
        "properties": {
          "Name": {
            "type": "string"
          },
          "UseTime": {
            "type": "string"
          },
          "Degree": {
            "type": "integer"
          }
        }
      },
      "TxtContent": {
        "type": "string",
        "analyzer": "ik",
        "include_in_all": true
      },
      "OtherContent": {
        "type": "string",
        "analyzer": "ik",
        "include_in_all": true
      }
    }
  }
}
Es模板

 

5. 获取映射信息: Get ~/<index>/_mapping/<type>

6. 查看所有的索引及类型信息: Get ~/_mapping

7. 查看状态:

http://blog.chinaunix.net/uid-532511-id-4854331.html

健康状态: curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'

8. 集群状态: 

curl -XGET 'http://localhost:9200/_cluster/state?pretty'

curl -XGET 'http://localhost:9200/_cluster/stats?human&pretty'

9. 节点状态:

curl -XGET 'http://localhost:9200/_nodes/stats?pretty'

curl -XGET 'http://localhost:9200/_nodes/stats/os,process?pretty'

curl -XGET 'http://localhost:9200/_nodes?pretty'
curl -XGET 'http://localhost:9200/_nodes/process?pretty'
curl -XGET 'http://localhost:9200/_nodes/os?pretty'
curl -XGET 'http://localhost:9200/_nodes/settings?pretty'

10. 本机节点状态:

curl -XGET 'localhost:9200/_nodes/_local?pretty'
curl -XGET 'localhost:9200/_nodes/_local/network?pretty'

 

查询也可以针对子节点进行:

curl -XGET 'http://localhost:9200/_nodes/子节点?pretty'

 

Mapping

官网类型定义:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-intro.html

官网复合类型: https://www.elastic.co/guide/en/elasticsearch/guide/current/complex-core-fields.html

复合类型是指,值是:数组,对象。

任何值,都可以按定义的数据类型保存为一个值,或一个该值的数据。

 

知识点

查询

http://www.cnblogs.com/zhangchenliang/p/4195406.html

 

排序 function_score函数

官方:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

文章: http://blog.csdn.net/dm_vincent/article/details/42201789

 

权重

字段后使用 ^2 表示该字段权限是2 如:

query:"title:中国^2+title:北京-content:美国"  表示:title中包含中国的部分权重是2 ,同时 title中包含北京,同时, content中不包含 美国。

 

返回列

_source 返回该节点及子节点

"_source": {
"Id": "8a7229609bd841fcb7ed2ffc68a5a01f",
"PersonInfo": {
"Name": "马少明"
}
},

fields 只能返回叶子节点

"fields": {
"PersonInfo.Name": [
"马少明"
],
"Id": [
"8a7229609bd841fcb7ed2ffc68a5a01f"
]
},

 

问题

1. 提交的Json,不能带有如下属性 :  id , _id , 但可以是: Id ,ID !

ES里的系统字段包含: _index,_type,_id,_score

2. 提交的Json,第一次提交,会建立一个模板,如果第二次提交的数据与第一次提交的数据在格式有冲突,则不能创建。

典型的例子是,第一次提交的数据是,有一个对象数组,但值是空的。 第二次提交的对象数据有值,则不能提交。

需要修改数据模板。

修改索引: 

http://www.cnblogs.com/Creator/p/3722408.html

http://www.cnblogs.com/tianjixiaoying/p/4424076.html

3.健康值红了:

http://localhost:9200/wy/_optimize 

4. 使用 function_score 的文章: http://blog.csdn.net/dm_vincent/article/details/42201721 , 它里面写的: Get _search 是错的,应该是 Post _search

Post ~/_search   or  ~/wy/_search  or ~/wy/hr/_search

{
  "query": {
    "function_score": {
      "filter": {
        "term": {
          "BirthYear": "1980"
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "NowCity": "北京"
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "sum"
    }
  }
}

 

你可能感兴趣的:(Elasticsearch笔记)