<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elasticsearch &#8211; wqh博客</title>
	<atom:link href="https://wangqianhong.com/tag/elasticsearch/feed/" rel="self" type="application/rss+xml" />
	<link>https://wangqianhong.com</link>
	<description>和而不同</description>
	<lastBuildDate>Fri, 27 Sep 2024 03:38:48 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wangqianhong.com/wp-content/uploads/2020/09/cropped-1-1-1-32x32.png</url>
	<title>Elasticsearch &#8211; wqh博客</title>
	<link>https://wangqianhong.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Elasticsearch（十）&#124; Docker证书部署</title>
		<link>https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/</link>
					<comments>https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Sat, 13 Aug 2022 02:53:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=3232</guid>

					<description><![CDATA[<p>使用Docker部署Elasticsearch，并配置证书，使用https访问。 在一个新的空目录中&#8230; <a href="https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（十）&#124; Docker证书部署</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/">Elasticsearch（十）| Docker证书部署</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>使用Docker部署Elasticsearch，并配置证书，使用https访问。</p>



<p>在一个新的空目录中，创建以下四个文件：</p>



<ul><li>instances.yml</li><li>.env</li><li>create-certs.yml</li><li>docker-compose.yml</li></ul>



<h3>instances.yml</h3>



<pre class="wp-block-code"><code>instances:
  - name: es01
    dns:
      - es01 
      - localhost
    ip:
      - 127.0.0.1</code></pre>



<h3>.env</h3>



<pre class="wp-block-code"><code>COMPOSE_PROJECT_NAME=es 
CERTS_DIR=/usr/share/elasticsearch/config/certificates 
ELASTIC_PASSWORD=你的密码</code></pre>



<h3>create-certs.yml</h3>



<pre class="wp-block-code"><code>services:
  create_certs:
    container_name: create_certs
    image: elasticsearch:7.17.22
    command: >
      bash -c '
        if &#91;&#91; ! -f /certs/bundle.zip ]]; then
          bin/elasticsearch-certutil cert --silent --pem --in config/certificates/instances.yml -out /certs/bundle.zip;
          unzip /certs/bundle.zip -d /certs; 
        fi;
        chown -R 1000:0 /certs
      '
    user: "0"
    working_dir: /usr/share/elasticsearch
    volumes: &#91;'certs:/certs', '.:/usr/share/elasticsearch/config/certificates']

volumes: {"certs"}</code></pre>



<h3>docker-compose.yml</h3>



<pre class="wp-block-code"><code>services:
  es01:
    container_name: es01
    image: elasticsearch:7.17.22
    environment:
      - LANG=C.UTF-8
      - LC_ALL=C.UTF-8
      - node.name=es01
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02
      - ELASTIC_PASSWORD=$ELASTIC_PASSWORD 
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - xpack.license.self_generated.type=trial 
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=$CERTS_DIR/es01/es01.key
      - xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
      - xpack.security.http.ssl.certificate=$CERTS_DIR/es01/es01.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.verification_mode=certificate 
      - xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
      - xpack.security.transport.ssl.certificate=$CERTS_DIR/es01/es01.crt
      - xpack.security.transport.ssl.key=$CERTS_DIR/es01/es01.key
    volumes: &#91;'data01:/var/lib/elasticsearch/data', 'certs:$CERTS_DIR']
    ports:
      - 9200:9200
    healthcheck:
      test: curl --cacert $CERTS_DIR/ca/ca.crt -s https://localhost:9200 >/dev/null; if &#91;&#91; $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 30s
      timeout: 10s
      retries: 5


  wait_until_ready:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.22
    command: /usr/bin/true
    depends_on: {"es01": {"condition": "service_healthy"}}

volumes: {"data01", "certs"}</code></pre>



<h3>执行命令</h3>



<p>生成证书（只需要一次）：</p>



<pre class="wp-block-code"><code>docker compose -f create-certs.yml run --rm create_certs</code></pre>



<p>启动为SSL/TLS配置的Elasticsearch节点：</p>



<pre class="wp-block-code"><code>docker compose up -d</code></pre>



<p>通过证书，输入密码即可访问</p>



<pre class="wp-block-code"><code>curl --cacert /tmp/ca.crt -u elastic https://localhost:9200</code></pre>



<p></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/">Elasticsearch（十）| Docker证书部署</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2022/08/elasticsearch%ef%bc%88%e5%8d%81%ef%bc%89-docker%e8%af%81%e4%b9%a6%e9%83%a8%e7%bd%b2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（九）&#124; bulk(批处理)的命令行</title>
		<link>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/</link>
					<comments>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Sat, 27 Nov 2021 06:51:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2448</guid>

					<description><![CDATA[<p>批处理命令bulk可以在单个请求中一次执行多个索引或者删除更新操作，使用这种方式可以极大的提升索引性&#8230; <a href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（九）&#124; bulk(批处理)的命令行</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（九）| bulk(批处理)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>批处理命令bulk可以在单个请求中一次执行多个索引或者删除更新操作，使用这种方式可以极大的提升索引性能。</p>



<p>官方地址：<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html" target="_blank" rel="noreferrer noopener">https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html</a></p>



<h3>测试数据</h3>



<p>准备好测试数据，保存到bulk.json文件中：</p>



<pre class="wp-block-code"><code>{"create":{"_index":"test_record"}}
{"key":"中华人民共和国国籍","date":"2021-11-15T13:12:00","counts" : 4}
{"create":{"_index":"test_record"}}
{"key":"中华人民共和国国徽","date":"2021-11-16T13:12:00","counts" : 14}
{"delete":{"_index":"test_record","_id" : "pzK5HIQB8xpN1XtdHm7Z" }}
{"update":{"_index":"test","_id":"qjK5HIQB8xpN1Xtdy25q"}}
{"doc":{"date":"2021-12-15T14:12:00"}}</code></pre>



<p>这里一共4个命令，先是创建了国籍和国徽，删除了国歌，把国旗的时间进行了修改。</p>



<h3>Bulk批处理</h3>



<p>使用下面命令可以执行json文件：</p>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/_bulk?pretty  -H 'Content-Type: application/json' --data-binary @bulk.json</code></pre>



<p>bulk会按顺序执行命令，并且如果其中有一个命令错误，依然是执行完毕，并返回每个命令对应的执行结果：</p>



<pre class="wp-block-preformatted">{
  "took" : 3933,
  "errors" : false,
  "items" : [
    {
      "create" : {
        "_index" : "test_record",
        "_id" : "vNoNN4QBHJgRqN7h6HeV",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 21,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 22,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "delete" : {
        "_index" : "test_record",
        "_id" : "pzK5HIQB8xpN1XtdHm7Z",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 23,
        "_primary_term" : 4,
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 27,
        "_primary_term" : 4,
        "status" : 200
      }
    }
  ]
}</pre>



<h3>Replace替换</h3>



<p>有时候在update的时候可能需要整个替换，在bulk中可以用下面的命令格式：</p>



<pre class="wp-block-code"><code>{"update":{"_index":"search_record","_id":"vNoNN4QBHJgRqN7h6HeV"}}
{"script":{"source":"ctx._source=params","params":{"key" : "中华人民共和国国歌","date" : "2021-10-15T13:12:00","counts" : 1},"lang":"painless"}}</code></pre>



<p>这条命令就是把新加的国籍整个替换成国歌的内容</p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（九）| bulk(批处理)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-bulk%e6%89%b9%e5%a4%84%e7%90%86%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（八）&#124; search(搜索)的进阶</title>
		<link>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/</link>
					<comments>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Fri, 19 Nov 2021 13:04:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2469</guid>

					<description><![CDATA[<p>在使用elasticsearch进程中，经常需要用到一些类似范围的搜索。 返回查询数量 当使用sea&#8230; <a href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（八）&#124; search(搜索)的进阶</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/">Elasticsearch（八）| search(搜索)的进阶</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>在使用elasticsearch进程中，经常需要用到一些类似范围的搜索。</p>



<h3>返回查询数量</h3>



<p>当使用search的时候，默认只返回10条数据，可以通过size参数设置：</p>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"size":100,"query":{"match_all":{}}}'</code></pre>



<p>如果正确就会返回test_record的100条数据：</p>



<pre class="wp-block-preformatted">{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-12-15T14:12:00",
          "counts" : 2,
          "key" : "中华人民共和国国旗"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qDK5HIQB8xpN1Xtdcm4P",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-10-16T13:12:00",
          "counts" : 0,
          "key" : "中华人民共和国"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qTK5HIQB8xpN1Xtdm245",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 1,
          "key" : "中华人民共和国万岁"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "vNoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 3,
          "key" : "中华人民共和国国籍"
        }
      }
    ]
  }
}</pre>



<h3>分页查询</h3>



<p>使用数据库的时候，当查询结果太多的时候，可以用分页查询，在elasticsearch中也可以：</p>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"from":0,"size":2,"query":{"match_all":{}}}' </code></pre>



<p>from就是起始位置，size就是每次取的数量。</p>



<pre class="wp-block-preformatted">{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-12-15T14:12:00",
          "counts" : 2,
          "key" : "中华人民共和国国旗"
        }
      }
    ]
  }
}</pre>



<h3>查询排序</h3>



<p>使用sort可以对查询进行排序：</p>



<pre class="wp-block-code"><code>url --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"from":0,"size":2,"query":{"match_all":{}},"sort":&#91;{"counts":{"order":"desc"}}]}'</code></pre>



<p>sort是一个数组类型的，可以实现对多个字段的综合排序</p>



<pre class="wp-block-preformatted">{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : null,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        },
        "sort" : [
          11
        ]
      },
      {
        "_index" : "test_record",
        "_id" : "vNoNN4QBHJgRqN7h6HeV",
        "_score" : null,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 3,
          "key" : "中华人民共和国国籍"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}</pre>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/">Elasticsearch（八）| search(搜索)的进阶</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ab%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e8%bf%9b%e9%98%b6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（七）&#124; update(更新)的命令行</title>
		<link>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/</link>
					<comments>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Sat, 13 Nov 2021 07:36:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2454</guid>

					<description><![CDATA[<p>update是常用操作，官方地址：https://www.elastic.co/guide/en/e&#8230; <a href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（七）&#124; update(更新)的命令行</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/">Elasticsearch（七）| update(更新)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>update是常用操作，官方地址：<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html" target="_blank" rel="noreferrer noopener">https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html</a></p>



<h3>直接通过id更新(_update)</h3>



<p>最直接的就是通过id进行更新：</p>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_update/qDK5HIQB8xpN1Xtdcm4P?pretty  -H 'Content-Type: application/json' -d '{"script":{"source":"ctx._source.counts--","lang":"painless"}}'</code></pre>



<p>上面的命令会把id为qDK5HIQB8xpN1Xtdcm4P，字段为counts的值减1。</p>



<p>如果要同时修改多个id要怎么做呢？直接用{}即可。</p>



<pre class="wp-block-code"><code>https:&#47;&#47;localhost:9200/test_record/_update/{qDK5HIQB8xpN1Xtdcm4P,qTK5HIQB8xpN1Xtdm245}?pretty</code></pre>



<p>如果正确返回下面的结果：</p>



<pre class="wp-block-preformatted">{
  "_index" : "test_record",
  "_id" : "vdoNN4QBHJgRqN7h6HeV",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 31,
  "_primary_term" : 4
}
{
  "_index" : "test_record",
  "_id" : "qjK5HIQB8xpN1Xtdy25q",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 32,
  "_primary_term" : 4
}</pre>



<h3>通过查询id更新(_update_by_query)</h3>



<p>上面的id也可以通过查询进行更新操作：</p>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_update_by_query?pretty  -H 'Content-Type: application/json' -d '{"query":{"terms":{"_id":&#91;"qDK5HIQB8xpN1Xtdcm4P","qTK5HIQB8xpN1Xtdm245"]}},"script":{"source":"ctx._source.counts--","lang":"painless"}}'</code></pre>



<p>和上面的效果是一样的，不过从性能上来说上面是发送了2条update命令，更这里只发送了1条update命令。从返回结果也可以看出区别</p>



<pre class="wp-block-preformatted">{
  "took" : 240,
  "timed_out" : false,
  "total" : 2,
  "updated" : 2,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}</pre>



<h3>通过查询关键字更新(_update_by_query)</h3>



<pre class="wp-block-code"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_update_by_query?pretty  -H 'Content-Type: application/json' -d'{"query":{"match":{"key":"中华人民共和国"}},"script":{"source":"ctx._source.counts--","lang":"painless"}}'</code></pre>



<p>这条命令是把含关键字为中华人民共和国记录的counts都减1，如果正确就会返回下面的结果</p>



<pre class="wp-block-preformatted">{
  "took" : 184,
  "timed_out" : false,
  "total" : 5,
  "updated" : 5,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}</pre>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/">Elasticsearch（七）| update(更新)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e4%b8%83%ef%bc%89-update%e6%9b%b4%e6%96%b0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（六）&#124; search(搜索)的命令行</title>
		<link>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/</link>
					<comments>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Fri, 05 Nov 2021 16:01:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2403</guid>

					<description><![CDATA[<p>有了数据就可以开始搜索。 官方文档地址：https://www.elastic.co/guide/e&#8230; <a href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（六）&#124; search(搜索)的命令行</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（六）| search(搜索)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>有了数据就可以开始搜索。</p>



<p>官方文档地址：<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html" target="_blank" rel="noreferrer noopener">https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html</a></p>



<h3>准备测试数据</h3>



<p>为了进行检索，插入一些测试数据：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国","date":"2021-10-16T13:12:00", "counts":2}'</code>
<code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国万岁","date":"2021-11-15T13:12:00", "counts":3}'</code>
<code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国国旗","date":"2021-12-15T13:12:00", "counts":4}'</code></pre>



<h3>返回所有数据</h3>



<p>获取索引下所有的数据：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match_all":{}}}'</code>

如果成功返回下面内容
{
  "took" : 552,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "pzK5HIQB8xpN1XtdHm7Z",
        "_score" : 1.0,
        "_source" : {
          "mappings" : {
            "properties" : {
              "key" : "中华人民共和国国歌",
              "date" : "2021-10-15T13:12:00",
              "counts" : 1
            }
          }
        }
      },
      ...
    ]
  }
}</pre>



<h3>过滤返回</h3>



<p>有时候只需要返回部分字段，可以使用下面命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match_all":{}},"_source":["key"]}'</code>

如果成功返回下面内容
{
  "took" : 199,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "pzK5HIQB8xpN1XtdHm7Z",
        "_score" : 1.0,
        "_source" : {
          "key" : "中华人民共和国国歌"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qDK5HIQB8xpN1Xtdcm4P",
        "_score" : 1.0,
        "_source" : {
          "key" : "中华人民共和国"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qTK5HIQB8xpN1Xtdm245",
        "_score" : 1.0,
        "_source" : {
          "key" : "中华人民共和国万岁"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_score" : 1.0,
        "_source" : {
          "key" : "中华人民共和国国旗"
        }
      }
    ]
  }
}</pre>



<h3>关键字搜索</h3>



<p>通过关键字进行检索：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty -H 'Content-Type: application/json' -d '{"query":{"match":{"key":"国歌"}}}'</code>

如果成功返回下面内容
{
  "took" : 82,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3260206,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "pzK5HIQB8xpN1XtdHm7Z",
        "_score" : 1.3260206,
        "_source" : {
          "key" : "中华人民共和国国歌",
          "date" : "2021-10-15T13:12:00",
          "counts" : 1
        }
      }
    ]
  }
}</pre>



<p></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（六）| search(搜索)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/11/elasticsearch%ef%bc%88%e5%85%ad%ef%bc%89-search%e6%90%9c%e7%b4%a2%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（五）&#124; document(文档)的命令行</title>
		<link>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/</link>
					<comments>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Sat, 30 Oct 2021 03:15:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2397</guid>

					<description><![CDATA[<p>索引创建好之后，就可以插入数据，在Elasticsearch中，这样的数据叫document。 官方&#8230; <a href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（五）&#124; document(文档)的命令行</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（五）| document(文档)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>索引创建好之后，就可以插入数据，在Elasticsearch中，这样的数据叫document。</p>



<p>官方文档地址：<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html" target="_blank" rel="noreferrer noopener">https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html</a></p>



<h3>Create Document(创建文档)</h3>



<p>创建文档的命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/test_record/_doc?pretty -H 'Content-Type: application/json' -d '{"key": "中华人民共和国国歌","date":"2021-10-15T13:12:00", "counts":1}'</code>

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}</pre>



<h3>Get Document(获取文档)</h3>



<p>通过上面返回的_id获取文档的命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_doc/njKfHIQB8xpN1Xtdv241?pretty</code>

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "key" : "中华人民共和国国歌",
    "date" : "2021-10-15T13:12:00",
    "counts" : 1
  }
}</pre>



<h3>Delete Document(删除文档)</h3>



<p>通过_id删除文档：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XDELETE https://localhost:9200/test_record/_doc/njKfHIQB8xpN1Xtdv241?pretty</code>

如果成功返回下面内容
{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}</pre>



<p>删除之后再使用上面的Get命令就获取不到了，会返回下面的内容：</p>



<pre class="wp-block-preformatted">{
  "_index" : "test_record",
  "_id" : "njKfHIQB8xpN1Xtdv241",
  "found" : false
}</pre>



<p></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（五）| document(文档)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%94%ef%bc%89-document%e6%96%87%e6%a1%a3%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（四）&#124; Index(索引)的命令行</title>
		<link>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/</link>
					<comments>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Sat, 23 Oct 2021 14:01:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2384</guid>

					<description><![CDATA[<p>在Centos服务器上可以使用curl命令来与Elasticsearch交互。 官方文档地址：htt&#8230; <a href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（四）&#124; Index(索引)的命令行</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（四）| Index(索引)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>在Centos服务器上可以使用curl命令来与Elasticsearch交互。</p>



<p>官方文档地址：<a rel="noreferrer noopener" href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html</a></p>



<h3>Create Index(创建索引)</h3>



<p>创建索引的命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XPUT https://localhost:9200/test_record?pretty -H 'Content-Type: application/json' -d '{"mappings":{"properties":{"key":{"type":"text","analyzer":"ik_smart"},"date":{"type":"date"},"counts":{"type":"integer"}}}}'</code>

如果成功返回下面内容
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test_record"
}</pre>



<p>这里的mappings可以理解为数据库里的表结构。</p>



<p>&#8211;cacert，表示https证书所在目录</p>



<p>-u，表示Elasticsearch的用户账号</p>



<p>-XPUT，表示使用PUT方法</p>



<p>pretty，表示返回的json内容格式化输出</p>



<p>-H，表示传输的数据类型</p>



<p>-d，表示传入的json数据</p>



<p>这里定义了三个字段：</p>



<p>key，类型为text（文本），使用ik_smart模式进行分词，方便之后进行检索</p>



<p>date，类型为date（日期）</p>



<p>counts，类型为integer（整数）</p>



<h3>Show Index(显示索引)</h3>



<p>查看索引的命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record?pretty</code>

如果成功返回下面内容
{
  "test_record" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "counts" : {
          "type" : "integer"
        },
        "date" : {
          "type" : "date"
        },
        "key" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test_record",
        "creation_date" : "1666920919590",
        "number_of_replicas" : "1",
        "uuid" : "S94doWMQTWuwLLObEgzGIg",
        "version" : {
          "created" : "8040199"
        }
      }
    }
  }
}</pre>



<h3>Delete Index(删除索引)</h3>



<p>删除索引的命令：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XDELETE https://localhost:9200/test_record?pretty</code>

如果成功返回下面内容
{
  "acknowledged" : true
}</pre>



<p></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/">Elasticsearch（四）| Index(索引)的命令行</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e5%9b%9b%ef%bc%89-index%e7%b4%a2%e5%bc%95%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（三）&#124; 基本概念</title>
		<link>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/</link>
					<comments>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Tue, 12 Oct 2021 01:19:00 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2374</guid>

					<description><![CDATA[<p>在使用Elasticsearch之前，需要理解一些基本概念，官方文档地址https://www.el&#8230; <a href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（三）&#124; 基本概念</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/">Elasticsearch（三）| 基本概念</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>在使用Elasticsearch之前，需要理解一些基本概念，官方文档地址<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html" target="_blank" rel="noreferrer noopener">https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html</a>。</p>



<p>Elasticsearch是一个近乎实时（NRT）的搜索平台。这意味着从索引文档到可搜索文档的时间有一点延迟（通常是一秒）。通常有集群，节点，分片，副本等概念。</p>



<h3>集群(Cluster)</h3>



<p>集群(cluster)是一组具有相同cluster.name的节点集合，他们协同工作，共享数据并提供故障转移和扩展功能，当然一个节点也可以组成一个集群。</p>



<p>集群由唯一名称标识，默认情况下为“elasticsearch”。此名称很重要，因为如果节点设置为按名称加入集群的话，则该节点只能是集群的一部分。</p>



<p>确保不同的环境中使用不同的集群名称，否则最终会导致节点加入错误的集群。</p>



<p>集群状态通过 绿，黄，红 来标识，可以使用下面命令查看：</p>



<pre class="wp-block-preformatted"><code>curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/_cat/indices\?v</code>

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_map O3bp3-B0Tx12_umWqYyHPA 1 1 4 0 6.9kb 6.9kb
yellow open test_record QXPRPktzTG8g9SOwyj3a6g 1 1 48 0 830.1kb 830.1kb</pre>



<ul><li>绿色 &#8211; 一切都很好（集群功能齐全）。</li><li>黄色 &#8211; 所有数据均可用，但尚未分配一些副本（集群功能齐全）。</li><li>红色 &#8211; 某些数据由于某种原因不可用（集群部分功能）。</li></ul>



<h3>节点(Node)</h3>



<p>节点，一个运行的 ES 实例就是一个节点，节点存储数据并参与集群的索引和搜索功能。</p>



<p>就像集群一样，节点由名称标识，默认情况下，该名称是在启动时分配给节点的随机通用唯一标识符（UUID）。如果不需要默认值，可以定义所需的任何节点名称。此名称对于管理目的非常重要，您可以在其中识别网络中哪些服务器与 Elasticsearch 集群中的哪些节点相对应。</p>



<p>可以将节点配置为按集群名称加入特定集群。默认情况下，每个节点都设置为加入一个名为 cluster 的 elasticsearch 集群，这意味着如果您在网络上启动了许多节点并且假设它们可以相互发现 &#8211; 它们将自动形成并加入一个名为 elasticsearch 的集群。</p>



<h3>索引(Index)</h3>



<p>索引是具有某些类似特征的文档集合。例如，可以拥有店铺数据的索引，商品的一个索引以及订单数据的一个索引。</p>



<p>索引由名称标识（必须全部小写），此名称用于在对其中的文档执行索引，搜索，更新和删除操作时引用索引。例如上面的test_map和test_record。</p>



<h3>文档(Document)</h3>



<p>文档是可以建立索引的基本信息单元。例如，可以为单个客户提供文档，为单个产品提供一个文档，为单个订单提供一个文档。该文档以JSON（JavaScript Object Notation）表示，JSON是一种普遍存在的互联网数据交换格式。</p>



<p>在索引/类型中，可以根据需要存储任意数量的文档。请注意，尽管文档实际上驻留在索引中，但实际上必须将文档编入索引/分配给索引中的类型。</p>



<h3>分片(Shards)</h3>



<p>索引可能存储大量可能超过单个节点的硬件限制的数据。例如，占用1TB磁盘空间的十亿个文档的单个索引可能不适合单个节点的磁盘，或者可能太慢而无法单独从单个节点提供搜索请求。</p>



<p>为了解决这个问题，Elasticsearch 提供了将索引细分为多个称为分片的功能。创建索引时，只需定义所需的分片数即可。每个分片本身都是一个功能齐全且独立的“索引”，可以托管在集群中的任何节点上。</p>



<p>设置分片的目的及原因主要是：</p>



<ul><li>它允许水平拆分/缩放内容量</li><li>它允许跨分片（可能在多个节点上）分布和并行化操作，从而提高性能/吞吐量</li><li>分片的分布方式以及文档聚合搜索请求的机制完全由 Elasticsearch 管理，对用户而言是透明的</li></ul>



<p>在可能随时发生故障的网络/云环境中，分片非常有用，建议使用故障转移机制，以防分片/节点以某种方式脱机或因任何原因消失。为此，Elasticsearch 允许将索引的分片的一个或多个副本制作成所谓的副本分片或简称副本。</p>



<h3>副本(Replicasedit)</h3>



<p>副本，是对分片的复制。目的是为了当分片/节点发生故障时提供高可用性，它允许扩展搜索量/吞吐量，因为可以在所有副本上并行执行搜索。</p>



<p>总而言之，每个索引可以拆分为多个分片。索引也可以复制为零次（表示没有副本）或更多次。复制之后，每个索引将具有主分片(从原始分片复制而来的)和复制分片(主分片的副本)。</p>



<h3>总结</h3>



<p>把Elasticsearch类比关系数据库，比如MySQL，可以得出下面的结论：</p>



<pre class="wp-block-preformatted">关系型数据库 -&gt; Databases(库) -&gt; Tables(表) -&gt; Rows(行) -&gt; Columns(列)。
Elasticsearch -&gt; Index(索引) -&gt; Mapping(映射) -&gt; Documents(文档) -&gt; Fields(属性)。</pre>



<p>Elasticsearch集群可以包含多个索引(indices)（数据库），每一个索引可以包含多个映射(Mapping)（表），每一个类型包含多个文档(documents)（行），然后每个文档包含多个字段(Fields)（列）。虽然这么类比，但毕竟这是两个差异化的产品。</p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/">Elasticsearch（三）| 基本概念</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%b8%89%ef%bc%89-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（二）&#124;  IK中文分词器安装</title>
		<link>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/</link>
					<comments>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Thu, 07 Oct 2021 07:45:58 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2291</guid>

					<description><![CDATA[<p>在使用Elasticsearch 进行搜索中文时，Elasticsearch 内置的分词器会将所有的&#8230; <a href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（二）&#124;  IK中文分词器安装</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/">Elasticsearch（二）|  IK中文分词器安装</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>在使用Elasticsearch 进行搜索中文时，Elasticsearch 内置的分词器会将所有的汉字切分为单个字，对于习惯中文的一些形容词、常见名字等无法优雅的处理，此时就需要用到一些开源的分词器，目前用的最多的就是IK分词器（项目地址：<a rel="noreferrer noopener" href="https://github.com/medcl/elasticsearch-analysis-ik" target="_blank">https://github.com/medcl/elasticsearch-analysis-ik</a>）</p>



<h3>安装</h3>



<p>IK分词器的安装十分简单，使用下面命令即可：</p>



<pre class="wp-block-code"><code>elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.4.1</code></pre>



<p>注意<strong>这里的版本要和Elasticsearch版本一致</strong>，否则会报错</p>



<pre class="wp-block-preformatted">java.lang.IllegalArgumentException: Plugin [analysis-ik] was built for Elasticsearch version 8.4.1 but version 8.4.2 is running</pre>



<p>当看到下面的输出就表示安装完成：</p>



<pre class="wp-block-preformatted">-&gt; Installed analysis-ik
-&gt; Please restart Elasticsearch to activate any plugins installed</pre>



<p>重启Elasticsearch即可</p>



<pre class="wp-block-code"><code>systemctl restart elasticsearch</code></pre>



<h3>模式</h3>



<p>ik_max_word: 会将文本做最细粒度的拆分，比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”，会穷尽各种可能的组合，适合 Term Query；</p>



<p>ik_smart: 会做最粗粒度的拆分，比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”，适合 Phrase 查询。</p>



<h3>自定义词库</h3>



<p>如果要让分词器支持一些专有词语，可以自定义词库。</p>



<p>IK分词器自带一个main.dic的文件，此文件为词库文件，这个文件在<code>/etc/elasticsearch/analysis-ik/main.dic</code>。</p>



<p>注意这个文件格式必须为utf-8（不要选择utf-8 BOM）。</p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/">Elasticsearch（二）|  IK中文分词器安装</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/10/elasticsearch%ef%bc%88%e4%ba%8c%ef%bc%89-ik%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e5%99%a8%e5%ae%89%e8%a3%85/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elasticsearch（一）&#124; Centos 安装 Elasticsearch 8.4</title>
		<link>https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/</link>
					<comments>https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/#respond</comments>
		
		<dc:creator><![CDATA[wqh_work]]></dc:creator>
		<pubDate>Thu, 30 Sep 2021 09:53:02 +0000</pubDate>
				<category><![CDATA[技术文章]]></category>
		<category><![CDATA[Centos]]></category>
		<category><![CDATA[Elasticsearch]]></category>
		<guid isPermaLink="false">https://wangqianhong.com/?p=2270</guid>

					<description><![CDATA[<p>Elaticsearch是一个开源的高扩展的分布式全文检索引擎，它可以近乎实时的存储、检索数据，官网&#8230; <a href="https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/" class="more-link read-more" rel="bookmark">继续阅读 <span class="screen-reader-text">Elasticsearch（一）&#124; Centos 安装 Elasticsearch 8.4</span><i class="fa fa-arrow-right"></i></a></p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/">Elasticsearch（一）| Centos 安装 Elasticsearch 8.4</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>Elaticsearch是一个开源的高扩展的分布式全文检索引擎，它可以近乎实时的存储、检索数据，官网地址：<a href="https://www.elastic.co/cn/" target="_blank" rel="noreferrer noopener">https://www.elastic.co/cn/</a></p>



<h3>安装</h3>



<p>Elasticsearch8.4自带了openjdk，所以不需要再安装java环境</p>



<p>首先安装GPG-KEY</p>



<pre id="block-3328977b-ca1e-4c74-9c5c-0eac6d5790e9" class="wp-block-code"><code>rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch</code></pre>



<p>第一种方式，使用root用户，进入目录<code>/etc/yum.repos.d/</code>，新建文件<code>elasticsearch.repo</code>，内容如下 ：</p>



<pre class="wp-block-code"><code>&#91;elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=0
type=rpm-md</code></pre>



<p>最后用yum命令安装即可：</p>



<pre class="wp-block-code"><code>yum install --enablerepo=elasticsearch elasticsearch</code></pre>



<p>系统会自动完成安装最新release版本</p>



<p>第二种方式，使用root用户wget手动下载需要的版本进行安装</p>



<pre class="wp-block-preformatted"><code>wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.4.1-x86_64.rpm</code>
<code>wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.4.1-x86_64.rpm.sha512</code>
<code>shasum -a 512 -c elasticsearch-8.4.1-x86_64.rpm.sha512</code>
<code>rpm --install elasticsearch-8.4.1-x86_64.rpm</code></pre>



<p>如果找不到命令shasum command not found，请先执行下面命令安装shasum</p>



<pre class="wp-block-code"><code>yum install perl-Digest-SHA -y</code></pre>



<h3>配置</h3>



<p>安装完成之后，可以找到系统自动生成的密码</p>



<pre class="wp-block-preformatted">The generated password for the elastic built-in superuser is : OKzrWoarTW8mnpfTcf_z</pre>



<p>也可以使用<code>/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic</code>重新自动生成密码</p>



<p>使用systemctl执行命令</p>



<pre class="wp-block-preformatted"><code>systemctl enable elasticsearch.service</code></pre>



<p>这里需要修改<code>/usr/lib/systemd/system/elasticsearch.service</code>中的TimeoutStartSec和ExecStart</p>



<p>ExecStart默认是前台启动，这里需要改成后台启动，不然用systemctl启动之后，到了TimeoutStartSec的时间，Elasticsearch服务就会超时关闭</p>



<pre class="wp-block-code"><code>ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -d -p ${PID_DIR}/elasticsearch.pid</code></pre>



<p>TimeoutStartSec默认在是75秒，可以改成150秒，因为第一次加载的时间比较长，否则如果用<code>systemctl start elasticsearch.service</code>命令启动 ，会报错Start operation timed out. Terminating</p>



<pre class="wp-block-code"><code> TimeoutStartSec=150</code></pre>



<p>修改<code>/etc/elasticsearch/elasticsearch.yml</code>，注释掉<code>cluster.initial_master_nodes</code>，并把集群设置为单结点</p>



<pre class="wp-block-code"><code>cluster.name: test-cluster
node.name: test-node-1
discovery.type: single-node
#cluster.initial_master_nodes: &#91;"9a1ae7a91a36"]</code></pre>



<h3>运行</h3>



<p>一切就绪就好，就可以启动Elasticsearch</p>



<pre class="wp-block-code"><code>systemctl daemon-reload
systemctl start elasticsearch.service</code></pre>



<p>启动完成之后，systemctl有可能提示失败，这不影响Elasticsearch，可以使用命令查看Elasticsearch是否启动成功</p>



<pre class="wp-block-preformatted"><code>curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200</code>

输入上面自动生成的密码，看到如下的输出就表示启动成功了：
{
  "name" : "test-node-1",
  "cluster_name" : "test-cluster",
  "cluster_uuid" : "2_iEDUKHSR-EqsYkAKlZog",
  "version" : {
    "number" : "8.4.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "2bd229c8e56650b42e40992322a76e7914258f0c",
    "build_date" : "2022-08-26T12:11:43.232597118Z",
    "build_snapshot" : false,
    "lucene_version" : "9.3.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}</pre>



<p>如果启动失败，可以查看<code>/var/log/elasticsearch/test-cluster.log</code>日志文件来追踪问题。</p>
<p><a rel="nofollow" href="https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/">Elasticsearch（一）| Centos 安装 Elasticsearch 8.4</a>最先出现在<a rel="nofollow" href="https://wangqianhong.com">wqh博客</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wangqianhong.com/2021/09/elasticsearch%ef%bc%88%e4%b8%80%ef%bc%89centos-%e5%ae%89%e8%a3%85-elasticsearch-8-4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
