码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearch-查询

时间:2016-07-19 23:39:36      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

使用如下语句创建一个名字为:user_document,别名为user的索引

PUT:http://localhost:9200/user_document

技术分享
{
     "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    },
    "mappings" : {
        "user_1" : {
            "properties" : {
                "Identity" : 
                { 
                    "type" : "string", 
                    "index" : "not_analyzed" 
                },
                 "Name" : 
                { 
                    "type" : "string"
                },
                 "Age" : 
                { 
                    "type" : "integer"
                },
                 "Birthday" : 
                { 
                    "format": "dateOptionalTime",
                    "type": "date"
                },
                 "Sex" : 
                { 
                    "type" : "boolean"
                }
            }
        }
    },
    "aliases" : {
        "user" : {}
    }
}
View Code

并新增10条测试数据

1、Match All 查询

技术分享
GET _search
{
    "query": 
    {
        "match_all": {}
    }
}
View Code

匹配索引中的全部文档,默认得分为1.0

2、Match 查询

技术分享
GET _search
{
    "query": 
    {
        "match": 
        {
            "Name":"陈楚生" 
        }
    }
}
View Code

结果

技术分享
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1.8677558,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1.8677558,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcPTKOpxK9K1OdHXH",
            "_score": 0.6856395,
            "_source": {
               "Identity": "360501199001016212",
               "Name": "陈楚河",
               "Age": 40,
               "Birthday": "1978-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcnK7OpxK9K1OdHXL",
            "_score": 0.14631948,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "陈乔恩",
               "Age": 36,
               "Birthday": "1980-05-09T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}
View Code

Match查询字符串时,会将文本内容分词并做Or操作

3、Multi Match 查询

技术分享
GET _search
{
    "query": 
    {
        "multi_match": {
           "query": "陈楚生",
           "fields": ["Name","Identity"]
        }
    }
}
View Code

同Match查询,但是可以同时查多个字段

4、Query String 查询

技术分享
GET _search
{
    "query": 
    {
        "query_string": {
           "default_field": "Name",
           "query": "陈楚生"
        }
    }
}
View Code

结果

技术分享
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1.8677558,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1.8677558,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcPTKOpxK9K1OdHXH",
            "_score": 0.6856395,
            "_source": {
               "Identity": "360501199001016212",
               "Name": "陈楚河",
               "Age": 40,
               "Birthday": "1978-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcnK7OpxK9K1OdHXL",
            "_score": 0.14631948,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "陈乔恩",
               "Age": 36,
               "Birthday": "1980-05-09T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}
View Code

分词查询,也可以想同时查询多个字段

5、Term 查询

技术分享
GET _search
{
    "query": 
    {
        "term": {
           "Identity": {
              "value": "360501199001016210",
              "boost": 2
           }
        }
    }
}
View Code

结果

技术分享
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 2.5040774,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 2.5040774,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}
View Code

Term查询一般用来做精确查询,即查询关键字需要和被查询字段值完全一致才会被查询出来。

所以如果当被查询的字段是一个String类型时,该类型需要被显示标记为not_analyzed,

否则字段内容会先被分词器分成许多个词条,导致term查询不出结果。

可以使用Match 或Query_String查询会被分词的字段。

6、Terms 查询

技术分享
GET _search
{
    "query": 
    {
        "terms": {
           "Age": [
              35,30
           ]
        }
    }
}
View Code

结果

技术分享
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.8853251,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 0.8853251,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDc4xqOpxK9K1OdHXN",
            "_score": 0.8853251,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "唐嫣",
               "Age": 30,
               "Birthday": "1986-05-09T14:12:12",
               "Sex": true
            }
         }
      ]
   }
}
View Code

查询一个值数组范围内的所有文档

7、Range 查询

技术分享
GET _search
{
    "query": 
    {
        "range": {
           "Age": {
              "from": 25,
              "to": 35
           }
        }
    }
}
View Code

结果

技术分享
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDch_LOpxK9K1OdHXK",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "赵默笙",
               "Age": 28,
               "Birthday": "1988-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDc4xqOpxK9K1OdHXN",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "唐嫣",
               "Age": 30,
               "Birthday": "1986-05-09T14:12:12",
               "Sex": true
            }
         }
      ]
   }
}
View Code

范围查询

 

elasticsearch-查询

标签:

原文地址:http://www.cnblogs.com/Jabben/p/5683060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!