elasticsearch应用与实战

自古美人都是妖i 提交于 2020-01-08 01:47:42

满足中文、拼音、首字母数字等检索用户信息。

准备工作

安装es,logstash,es-head(可以不安装,只是一个集群监控系统)注意版本兼容,我都用的5版本。

添加拼音分词、中文分词插件(手动把jar放到如下位置即可)

创建索引

例子只对用户名做了检索

PUT /user
{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "pinyin_analyzer" : {
                    "tokenizer" : "my_pinyin"
                    }
            },
            "tokenizer" : {
                "my_pinyin" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : false,
                    "keep_full_pinyin" : true,
					"keep_joined_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true
                }
            }
        }
    },
    "mappings": {
    "user":{
      "properties": {
        "dept_id":{
          "type": "integer"
        },
        "id":{
          "type": "integer"
        },
        "real_name":{
          "type": "keyword",
          "fields": {
                    "pinyin": {
                        "type": "text",
                        "store": false,
                        "term_vector": "with_offsets",
                        "analyzer": "pinyin_analyzer",
                        "boost": 10
                    },
					"ik":{
						"type": "text",
						"analyzer": "ik_max_word",
						"search_analyzer": "ik_smart"
					}
                }
        },
        "position":{
          "type": "string"
        },
        "phone":{
          "type": "string"
        },
        "job_number":{
          "type": "string"
        }
      }
    }
  }
}

logstash

新建user.conf到D:\logstash-5.1.1\logstash-5.1.1\config,这里只简单做了一个粗暴的同步。

input {
  jdbc {
    jdbc_driver_library => "D:\logstash-5.1.1\logstash-5.1.1\config\mysql-connector-java-5.1.35.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/test"
    jdbc_user => "root"
	jdbc_password => "root"
    schedule => "* * * * *"
    statement => "SELECT * from user"
  }
}
output {
    elasticsearch {
        hosts => ["127.0.0.1:9200","127.0.0.1:9201"]
        index => "org"
		document_type => "user"
        template_overwrite => true
		document_id => "%{id}"
    }
}

springboot依赖

<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>

application.properties

#集群端口的主机地址
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300,127.0.0.1:9301
spring.data.elasticsearch.cluster-name=my-application

核心代码

String keyword = "";
        if (!StringUtils.isBlank(searchOrgReq.getKeyword()))
            keyword = searchOrgReq.getKeyword().toLowerCase();
        //should是或者的关系
        BoolQueryBuilder keywordQueryBuilder = QueryBuilders.boolQuery();
        keywordQueryBuilder.should(QueryBuilders.termQuery("name.pinyin", keyword));
        keywordQueryBuilder.should(QueryBuilders.termQuery("name.ik", keyword));
        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(StringUtils.isBlank(keyword) ? QueryBuilders.matchAllQuery() : keywordQueryBuilder);
        nativeSearchQuery.addIndices("user");
        nativeSearchQuery.setPageable(PageRequest.of(0, 1000));
        SearchHits searchHits = elasticsearchTemplate.query(nativeSearchQuery, SearchResponse::getHits);

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!