怒写一篇基于Windows10的最新ElasticSearch5.3的搜索引擎的开发博客( 二)

鉴于jest已经封装了ES而且ES也算是对lucene的封装。因为版本太新,网上找不到我要的东西,所以我今天专门看了一天官网的文档,在此做一个笔记。

上次的项目因为忘记备份宕机之后我又重新做了一份,因此也发现了一些新的问题。首先解决上次的问题

比如在进行完全匹配(termQuery)查询的时候,会遇到查询结果为空的现象,我们在前面name属性下配置的ik_max_word,会让搜索的时候也进行中文分词,那么如果你输入的文本不是一个可以被ik包含的分词对象,比如“中国人民解放军战士”,它的分词中“中国人民解放军”是最长的一个词,并不会成为默认分词里“中国人民解放军战士”的原样匹配。所以这点是需要注意的。

如果说你的搜索需要添加拼音分词器的话,可以去下载插件包https://github.com/medcl/elasticsearch-analysis-pinyin/releases  这里可能版本不太一致,不过简单,你只需要把属性文件里面的版本号改为你当前使用的ES版本即可。然后把解压的目录放到ES安装目录的plugins文件夹下面的pinyin文件夹下(如果没有则创建),重启ES服务。然后在head插件中复合查询输入

http://localhost:9200/index/_analyze?analyzer=pinyin&text=张学友" /

结果为:
{
"tokens": [
{
"token": "zhang",
"start_offset": 0,
"end_offset": 1,
"type": "word",
"position": 0
}
,
{
"token": "xue",
"start_offset": 1,
"end_offset": 2,
"type": "word",
"position": 1
}
,
{
"token": "you",
"start_offset": 2,
"end_offset": 3,
"type": "word",
"position": 2
}
,
{
"token": "zxy",
"start_offset": 0,
"end_offset": 3,
"type": "word",
"position": 3
}
]
}

表明拼音分词成功!


再有者就是很多博客都是直接从官网文档复制的代码块过来进行翻译,让很多初学者感到迷惑。特别是Windows开发者来说,可能有人会像我一样在dos命令中进行输入,而且还需要进行字符转换。除了上一篇我们说过的head插件里面的“复合查询”功能,还有就是用一个Console UI,这样其他博客或者是官网的代码就能直接复制运行。

在详细讲述ElasticSearch5.3之前我们先解释几个ES常用的参数如下:


  • took – ES执行搜索得到结果所需时间的毫秒数
  • timed_out –反映搜索是否超时
  • _shards – 反馈总共搜索的分片数,还有成功和失败分别多少分片
  • hits – 搜索结果
  • hits.total – 符合条件的搜索总量
  • hits.hits – 实际搜索结果(默认十条)
  • hits.sort - 排序关键字(如果按分数排序则失效)
pom.xml文件内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yfy.test</groupId>
  <artifactId>es_yfy</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>es_yfy</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
     <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.4</version>
            </dependency>
  </dependencies>
</project>

你们可以忽略commons-lang3包 我当时需要转换转义字符串的时候用到的StringEscapeUtils的时候用到的。

在使用ES的时候肯定需要客户端,搭建集群的之后需要创建一个客户端,代码如下:
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
这里的两个host分别是相应的机器的ip地址,注意端口要设置一致。我借助JFinal工具包写了一个用配置支持的类如下:
public class EsUtil {
	public static EsService esService;
	public static Settings settings;
	public static TransportClient client;
	public static Properties properties;
	public static String config = "config.properties";
	static{
	properties = PropKit.use(config, Const.DEFAULT_ENCODING).getProperties();
	String clusterName = properties.getProperty("cluster.name", "");//获取配置文件中的cluster.name没有就默认空串
	//client.transport.sniff这个选项是为了嗅探集群用的,根据官方文档的说明,嗅探是为了动态的添加新的机器或者移除老的机器
//所用的,设置为true的时候,将会连接由addTransportAddress添加的节点。客户端内部的节点列表仅会被这些数据节点代替。每五秒钟刷新一次
	settings = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true).build();
	String ip = properties.getProperty("ip", "localhost");
	int port = Integer.parseInt(properties.getProperty("port", "9300"));
	try {
		client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));

	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
	}
}

到此已经写完预备工作,增删改查以及聚合排序索引会继续更新。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值