设为首页 加入收藏

TOP

分布式搜索引擎Elasticsearch的查询与过滤(三)
2017-10-13 10:21:36 】 浏览:7884
Tags:分布式 搜索引擎 Elasticsearch 查询 过滤
quot; : 104541 } } ] } } 

显示指定字段,匹配关键字

结果格式

  • took – 搜索用的毫秒

  • timed_out – 搜索超时时间

  • _shards – 搜索了多少个片段 成功搜索多少个 失败了多少个

  • hits – 搜索的返回结果集

  • hits.total – 结果的总数

  • hits.hits – 实际搜索返回的数组

  • _score and max_score - ignore these fields for now

一旦es搜索返回给你 该链接就断开了 es并不会想MySQL之类的 一直维护一个连接

 

更多限制条件的搜素

curl -XGET 'localhost:9200/test/_search?pretty' -d'{
    "from": 0, 
    "size": 1,
    "_source": ["title", "allnum"],
    "query": { 
        "match": { "title": "地铁跑酷" }
    },
   "sort": { "allnum": { "order": "desc" }}
}'

2、使用should

curl -XGET 'localhost:9200/test/_search?pretty' -d'{
    "from": 0, 
    "size": 2,
    "_source": ["title", "allnum"],
    "query": { 
        "bool": {
            "should": [
                { "match": { "title": "地铁跑酷" } },
                { "match": { "title": "星猫跑酷" } }
            ]
        }
    },
   "sort": { "allnum": { "order": "desc" }}
}'

3、使用must/must not

curl -XGET 'localhost:9200/test/_search?pretty' -d'{
    "from": 0, 
    "size": 2,
    "_source": ["title", "allnum"],
    "query": { 
        "bool": {
            "must": [
                { "match": { "title": "地铁跑酷" } },
                { "match": { "title": "星猫跑酷" } }
            ]
        }
    },
   "sort": { "allnum": { "order": "desc" }}
}'

当然 must /should/must not 可以联合起来使用 

4、过滤器

curl -XPOST 'localhost:9200/test/_search?pretty' -d'{
    "query": { 
        "filtered": {
            "query": { 
                 "match_all": {} 
              }, 
             "filter": { 
                 "range": { "allumn": { "gte": 20000, "lte": 30000 } } } } 
               }
}'

5、聚合

curl  -XPOST  'localhost:9200/test/_search?pretty' -d'{
    "size": 0, 
    "aggs": { 
        "group_by_state": { 
            "terms": { "field": "state" } }
         }
}'

6、terms 过滤

curl  -XPOST  'localhost:9200/test/_search?pretty' -d'{ 
  "query": { 
    "terms": { 
      "status": [ 
        304, 
        302 
      ] 
    } 
  } 
}

7、range 过滤

curl  -XPOST  'localhost:9200/test/_search?pretty' -d'{ 
  "query": { 
    "range": { 
      "allnum": { 
        "gt": 1 
      } 
    } 
  } 
}

范围操作符包含:

  • gt :: 大于

  • gte:: 大于等于

  • lt :: 小于

  • lte:: 小于等于

8、exists 和 missing 过滤

exists 和 missing 过滤可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件

{ 
    "exists":   { 
        "field":    "title" 
    } 
} 

9、bool 过滤

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

  • must :: 多个查询条件的完全匹配,相当于 and。
  • must_not :: 多个查询条件的相反匹配,相当于 not。
  • should :: 至少有一个查询条件匹配, 相当于 or。

这些参数可以分别继承一个过滤条件或者一个过滤条件的数组:

{ 
    "bool": { 
        "must":     { "term": { "folder": "inbox" }}, 
        "must_not": { "term": { "tag":    "spam"  }}, 
        "should": [ 
                    { "term": { "starred": true   }}, 
                    { "term": { "unread":  true   }} 
        ] 
    } 
}

七、中文分词

创建索引

curl -XPUT http
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Centos7安装完毕后无法联网的解决.. 下一篇(转)tomcat进程意外退出的问题分析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目