`

elasticsearch的dsl查询

阅读更多

     测试es的dsl查询,准备数据,在插入数据的时候,如果index、type、mapping都没有,es会自动创建

一、数据的准备

curl -XPOST "http://192.168.99.1:9200/new_index/product/p1" -d'
{
  "name":"new Mac Book"
}'

curl -XPOST "http://192.168.99.1:9200/new_index/product/p2" -d'
{
  "name":"note book",
  "price":12345,
  "cats":["notes"]
}'

curl -XPOST "http://192.168.99.1:9200/new_index/product/p3" -d'
{
  "name":"apples",
  "price":4,
  "cats":["fruit"]
}'

curl -XPOST "http://192.168.99.1:9200/new_index/product/p4" -d'
{
  "name":"pen",
  "price":50
}'

curl -XPOST "http://192.168.99.1:9200/new_index/product/p5?routing=p5" -d'
{
  "name":"add document use routing"
}'

curl -XPOST "http://192.168.99.1:9200/new_index/product/p6" -d'
{
  "name":"I love apples too.",
  "price":60
}'

二、修改name字段的mapping

       注意:1、mapping是要在创建索引的时候创建好,已经创建好的field的mapping不可再次修改,但是可以新增field的mapping

                  2、在es5中,需要设置text类型字段的fielddata=true,不然下方的查询会报错

curl -XPOST "http://192.168.99.1:9200/new_index/_mapping/product" -d'
{
  "properties": {
    "name":{
      "type": "text", 
      "fielddata": true
    }
  }
}'

三、查询出price在1-200之间,且name属性中必须不包含pen,按照price降序,name升序,对返回的结果进行分页,_source中的数据进行过滤,返回name字段分词后的term数据

        注意:已经分词过的字段不建议排序,耗性能,而且排序也不一定能得到想要的结果
    

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "explain": true, 
  "from": 0,
  "size": 20, 
  "fielddata_fields" : ["name"],
  "_source": {
    "include": ["name*","price"],
    "exclude": ["name"]
  },
  "version": true, 
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "price": {
              "gte": 1,
              "lte": 200
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "name": {
              "value": "pen"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "price": "desc"
    },
    {
      "name" : {
        "order": "asc"
      }
    }
  ]
}'

 四、统计price在1-200之间,且name属性中必须不包含pen的数据的数量

curl -XGET "http://192.168.99.1:9200/new_index/product/_count" -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "price": {
              "gte": 1,
              "lte": 200
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "name": {
              "value": "pen"
            }
          }
        }
      ]
    }
  }
}'

 五、查询price在1-200之间,且name属性中必须不包含pen的数据是否存在

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "size":0,
  "terminate_after":1,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "price": {
              "gte": 1,
              "lte": 200
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "name": {
              "value": "pen"
            }
          }
        }
      ]
    }
  }
}'

 六、查询id为p1,p3,p4的数据

 

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "query": {
    "ids": {
      "values": ["p1","p2","p3"]
    }
  }
}'
   如果上方的查询url中没有指定type,那么也是可以在ids下方指定type的

 七、查询文档中一定包含price字段的文档

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "query": {
    "exists" : {
      "field" : "price"
    }
  }
}'

 八、通配符查询,查询name字段是bo*k这种格式的

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "query": {
    "wildcard": {
      "name": {
        "value": "bo*k"
      }
    }
  }
}'

 九、query和filter一起结合使用

    需求:过滤出price在1-200之间,name中包含apples

    注意:1、query查询需要考虑到文档的相关性评分,而filter不需要。

               2、filter查询的结果会缓存,速度比query要快

               3、一般需要做全文检索的字段或需要考虑到相关度评分的字段使用query查询,其余的可以考虑使用filter过滤。

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "price": {
            "gte": 1,
            "lte": 200
          }
        }
      },
      "must": [
        {
          "term": {
            "name": {
              "value": "apples"
            }
          }
        }
      ]
    }
  }
}'

十、match匹配查询

    operator:

        and :  表示query中的数据分词后的都需要匹配上。

        or    :  表示query中的数据分词后任意一个匹配上即可。(默认)

    minimum_should_match : 里面的值写百分比,即query中的词分词后,满足minimum_should_match这个里面的百分比即可。默认最少需要匹配一个。(如果query中的词可以分成3个term,配置的百分比为80%,那么3*0.8最终需要匹配2个)
  
 十一、multi_match查询,从多个字段中查询

          注意:fields中写的是多个字段

                     type 的值有多个,不同的值会影响文档的得分

curl -XGET "http://192.168.99.1:9200/new_index/product/_search" -d'
{
  "query": {
    "multi_match": {
      "query": "apples",
      "fields": ["name","descs"],
      "type": "best_fields"
    }
  }
}'

 

 

  • 大小: 245.9 KB
  • 大小: 57.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics