zoukankan      html  css  js  c++  java
  • (51)ElasticSearch之query string查询及copy_to的使用

      1、准备数据

    PUT /myindex/article/1
    {
      "post_date":"2018-05-10",
      "title":"Java",
      "content":"java is the best language",
      "author_id":119
    }
    
    PUT /myindex/article/2
    {
      "post_date":"2018-05-12",
      "title":"html",
      "content":"I like html",
      "author_id":120
    }
    
    PUT /myindex/article/3
    {
      "post_date":"2018-05-16",
      "title":"es",
      "content":"Es is distributed document store",
      "author_id":110
    }

      2、操作演示

      1)查询myindex索引下所有文档

    GET /myindex/article/_search

      查询结果

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "2",
            "_score": 1,
            "_source": {
              "post_date": "2018-05-12",
              "title": "html",
              "content": "I like html",
              "author_id": 120
            }
          },
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "1",
            "_score": 1,
            "_source": {
              "post_date": "2018-05-10",
              "title": "Java",
              "content": "java is the best language",
              "author_id": 119
            }
          },
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "3",
            "_score": 1,
            "_source": {
              "post_date": "2018-05-16",
              "title": "es",
              "content": "Es is distributed document store",
              "author_id": 110
            }
          }
        ]
      }
    }

      2)查询myindex索引下日期是2018-05-10的文档。日期类型不会分词,要精确查询

    GET /myindex/article/_search?q=post_date:2018-05-10

      查询结果

    {
      "took": 31,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "1",
            "_score": 1,
            "_source": {
              "post_date": "2018-05-10",
              "title": "Java",
              "content": "java is the best language",
              "author_id": 119
            }
          }
        ]
      }
    }

      3)查询myindex索引下content字段中含有html的文档

    GET /myindex/article/_search?q=content:html

      查询结果

    {
      "took": 12,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "2",
            "_score": 0.2876821,
            "_source": {
              "post_date": "2018-05-12",
              "title": "html",
              "content": "I like html",
              "author_id": 120
            }
          }
        ]
      }
    }

      4)查询myindex索引下,字段中含有html或者document的文档,不指定字段,所有字段都会对比,性能低

    GET /myindex/article/_search?q=html,document

      查询结果

    {
      "took": 19,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 0.2876821,
        "hits": [
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "2",
            "_score": 0.2876821,
            "_source": {
              "post_date": "2018-05-12",
              "title": "html",
              "content": "I like html",
              "author_id": 120
            }
          },
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "3",
            "_score": 0.2876821,
            "_source": {
              "post_date": "2018-05-16",
              "title": "es",
              "content": "Es is distributed document store",
              "author_id": 110
            }
          }
        ]
      }
    }

      3、使用copy_to字段解决低性能问题

      1)该字段是把其他字段中的值,以空格为分隔符组成一个大字符串,然后被分析和索引但是不存储,也就是说它能被查询,但是不能被取回显示。

      2)注意copy_to指向的字段,字段类型要为text

      3)当没有指定查询的字段时,就会从copy_to字段中查询

      需要自己创建mapping,不能用默认的,下面创建mapping,先要删除掉之前的

    DELETE myindex
    PUT /myindex PUT /myindex/article/_mapping { "properties":{ "post_date":{ "type":"date" }, "title":{ "type":"text", "copy_to":"fullcontents" }, "content":{ "type":"text", "copy_to":"fullcontents" }, "author_id":{ "type":"integer" } } }

      准备数据,还用前面的

    PUT /myindex/article/1
    {
      "post_date":"2018-05-10",
      "title":"Java",
      "content":"java is the best language",
      "author_id":119
    }
    
    PUT /myindex/article/2
    {
      "post_date":"2018-05-12",
      "title":"html",
      "content":"I like html",
      "author_id":120
    }
    
    PUT /myindex/article/3
    {
      "post_date":"2018-05-16",
      "title":"es",
      "content":"Es is distributed document store",
      "author_id":110
    }

      查询:myindex索引下含有html、document的文档

    GET /myindex/article/_search?q=fullcontents:html,document

      查询结果

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 0.39556286,
        "hits": [
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "2",
            "_score": 0.39556286,
            "_source": {
              "post_date": "2018-05-12",
              "title": "html",
              "content": "I like html",
              "author_id": 120
            }
          },
          {
            "_index": "myindex",
            "_type": "article",
            "_id": "3",
            "_score": 0.2876821,
            "_source": {
              "post_date": "2018-05-16",
              "title": "es",
              "content": "Es is distributed document store",
              "author_id": 110
            }
          }
        ]
      }
    }
  • 相关阅读:
    Hadoop集群(三) Hbase搭建
    Hadoop集群(二) HDFS搭建
    Hadoop集群(一) Zookeeper搭建
    Redis Cluster 添加/删除 完整折腾步骤
    Redis Cluster在线迁移
    Hadoop分布式HA的安装部署
    Describe the difference between repeater, bridge and router.
    what is the “handover” and "soft handover" in mobile communication system?
    The main roles of LTE eNodeB.
    The architecture of LTE network.
  • 原文地址:https://www.cnblogs.com/javasl/p/12659280.html
Copyright © 2011-2022 走看看