Elastic Search对Document的搜索

在ES中使用的重点。ES中存储的数据。核心就是为了提供全文搜索能力的。搜索功能很是重要。多练。

php

1 query string search
search的参数都是相似http请求头中的字符串参数提供搜索条件的。
GET [/index_name/type_name/]_search[?parameter_name=parameter_value&...]
如: 全数据搜索。也就是没有搜索条件。
GET /test_index/my_type/_search
结果:java

{
    "took": 8, # 执行的时长。单位毫秒。
    "timed_out": false, # 是否超时
    "_shards": { # shard 相关数据
        "total": 5, # 总计多少个shard
        "successful": 5, # 成功返回结果的shard数量
        "skipped": 0,
        "failed": 0
    },
    "hits": { # 搜索结果相关数据,
        "total": 3, # 总计多少数据,符合搜索条件的数据数量。
        "max_score": 1, # 最大相关度分数。和搜索条件的匹配度。
        "hits": [ # 具体的搜索结果
            {
                "_index": "test_index", # 索引名称
                "_type": "my_type", # 类型名称
                "_id": "2", # id值
                "_score": 1, # 匹配度分数,本条数据匹配度分数
                "_source": { # 具体的数据内容,源
                    "name": "test_doc_02",
                    "remark": "second test elastic search",
                    "order_no": 2
                }
            }
        ]
    }
}

  

GET /index_name/type_name/_search?q=field_name:key_word&sort=field_name:order
如:
GET /test_index/my_type/_search?q=remark:test&sort=order_no:desc
结果:spa

{
    "took":17,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":3,
        "max_score":null,
        "hits":[
            {
                "_index":"test_index",
                "_type":"my_type",
                "_id":"3",
                "_score":null,
                "_source":{
                    "name":"test_doc_03",
                    "remark":"third test elastic search",
                    "order_no":3
                },
                "sort":[
                    3
                ]
            },
            {
                "_index":"test_index",
                "_type":"my_type",
                "_id":"2",
                "_score":null,
                "_source":{
                    "name":"test_doc_02",
                    "remark":"second test elastic search",
                    "order_no":2
                },
                "sort":[
                    2
                ]
            },
            {
                "_index":"test_index",
                "_type":"my_type",
                "_id":"1",
                "_score":null,
                "_source":{
                    "name":"test_doc_01",
                    "remark":"first test elastic search",
                    "order_no":1
                },
                "sort":[
                    1
                ]
            }
        ]
    }
}

注意:此搜索操做通常只用在快速检索数据使用,若是查询条件复杂,很难构建query string。生产环境中不多使用。如:要求搜索条件为商品名称包含手机,价格在1000~5000之间,销量在每个月500以上,根据价格升序排列,分页查询第二页,每页40条数据。
?q=xxxx:xxx&range=xxx:xxx:xxx&aggs&sort&from&size.net


2 query DSL
DSL - Domain Specified Language , 特殊领域的语言。blog

GET /index_name/type_name/_search
{
    "commond":{ "parameter_name" : "parameter_value"}
}

  


如:查询全部数据排序

GET /test_index/my_type/_search
{
    "query" : { "match_all" : {} }
}

  


如:条件查询,排序索引

GET /test_index/my_type/_search
{
    "query" : {
        "match" : {
            "remark" : "test"
        }
    },
    "sort" : [
        { "order_no" : "asc" }
    ]
}

  


如:分页查询ip

GET /test_index/my_type/_search
{
   "query" : { "match_all" : {} },
   "from" : 1, # 从第几条数据开始查询,从0开始计数
   "size" : 2, # 查询多少数据。
   "sort" : [
      { "order_no" : "asc" }
   ]
}

 


如:查询部分字段ci

GET /test_index/my_type/_search
{
    "query": {
        "match": {
            "tags": "java"
        }
    },
    "sort": [
        {
            "age": {
                "order": "desc"
            }
        }
    ],
    "_source": ["name", "tags"], 
    "from": 1,
    "size": 1
}

注意:此搜索操做适合构建复杂查询条件,生产环境经常使用。rem

3 query filter
过滤查询。此操做实际上就是query DSL的补充语法。过滤的时候,不进行任何的匹配分数计算,相对于query来讲,filter相对效率较高。Query要计算搜索匹配相关度分数。Query更加适合复杂的条件搜索。
如:使用符合条件查询。搜索tags中包含java字符串的数据,且年龄在20~25之间。
不使用filter, 年龄须要计算相关度分数GET /test_index/my_type/_search

{
     "query": {
         "bool": { # 多条件搜索,内部的若干条件,只要有正确结果,便可。
             "must": [ # 必须,内部若干条件,必须都匹配才有结果
                 {"match": { # 匹配, 字段中必须匹配对应数据才有结果
                     "tags": "java"
                 }},
                 {"range": { # 范围, 字段的数据必须知足某范围才有结果。
                     "age": {
                         "gte": 20, # 比较符号 lt gt lte gte
                         "lte": 25
                     }
                 }}
             ]
         }
    }
}

  

 

使用filter, 假设年龄不须要计算任何的相关度分数。

GET /test_index/my_type/_search
{
    "query": {
        "bool": { # 多条件搜索,内部的若干条件,只要有正确结果,便可。
        "must": [ # 必须,内部若干条件,必须都匹配才有结果
            {"match": { # 匹配, 字段中必须匹配对应数据才有结果
                "tags": "java"
            }},
            {"range": { # 范围, 字段的数据必须知足某范围才有结果。
                "age": {
                    "gte": 20, # 比较符号 lt gt lte gte
                    "lte": 25
                }
            }}
            ]
        }
    }
}

  


4 full-text search
全文检索。要求查询条件拆分后的任意词条与具体数据匹配就算搜索结果。查询结果顺序默认与匹配度分数相关。
搜索 tags中包含 java php .net的数据。

GET /test_index/my_type/_search
{
    "query": {
        "match": {
            "tags": "java php .net sales"
        }
    }
}

  

 

5 phrase search
短语检索。要求查询条件必须和具体数据彻底匹配才算搜索结果。

GET /test_index/my_type/_search
{
     "query": {
         "match_phrase": {
             "tags": "java developer"
         }
     }
}

  


6 highlight display
高亮显示。高亮不是搜索条件,是显示逻辑。在搜索的时候,常常须要对条件实现高亮显示。

GET /test_index/my_type/_search
{
    "query": {
        "match": {
            "tags": "java sales developer"
        }
    },
    "highlight": {
         "fields": {
             "tags": {
                 "number_of_fragments": 1,
                 "fragment_size": 1
             }
         }
    }
}