elasticsearch应用与实战

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

准备工作

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

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

创建索引

例子只对用户名做了中文和拼音的分词检索

PUT /org
{
    "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"
        },
        "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 = 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("org");
        nativeSearchQuery.setPageable(PageRequest.of(0, 1000));
        SearchHits searchHits = elasticsearchTemplate.query(nativeSearchQuery, SearchResponse::getHits);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值