Elasticsearch权威指南(中文版)-搜索章节的学习

in Elasticsearch with 0 comment

天空

构建请求

curl -i -XGET "Content-Type: application/json" -XPOST localhost:9200/_count?pretty -d '
{
    'user_name':"xiaoming"
}
'
curl -i -XGET "Content-Type: application/json" 'http://localhost:9200/_count?pretty' -d '
{
"query": {
    "match_all": {}
    }
}
'

添加索引

curl -XPUT  localhost:9200/megacorp/employee/1?pretty -H "Content-Type: application/json" -d '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'

curl -XPUT  localhost:9200/megacorp/employee/2?pretty -H "Content-Type: application/json" -d '
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}'

curl -XPUT  localhost:9200/megacorp/employee/3?pretty -H "Content-Type: application/json" -d '
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}'

简单搜索

curl -i -XGET "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/1?pretty'

curl -i -XGET "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty'

curl -i -XGET "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty&q=last_name:Smith'

DSL语句查询

curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}'

更复杂的查询

找到姓氏为“Smith”的员工,但是我们只想得到年龄大于30岁的员工
curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "bool" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } <1>
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "smith" <2>
                }
            }
        }
    }
}'

elaticsear no [query] registered for [filtered] 错误

使用bool / must / filter查询

curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "bool" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "must" : {
                "match" : {
                    "last_name" : "smith"
                }
            }
        }
    }
}'

全文搜索

curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}'

查询的结果集合

{
    ....
        "max_score" : 1.146971,
        "hits" : [
        {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 1.146971,
            "_source" : {
            "first_name" : "John",
            "last_name" : "Smith",
            "age" : 25,
            "about" : "I love to go rock climbing",
            "interests" : [
                "sports",
                "music"
            ]
            }
        },
        {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.28368035,
            "_source" : {
            "first_name" : "Jane",
            "last_name" : "Smith",
            "age" : 32,
            "about" : "I like to collect rock albums",
            "interests" : [
                "music"
            ]
            }
        }
        ]
    }
}
查询的结果中, 可以查看 _score , 它是根据匹配程度得出的一个分数, 例如上面的match查询中, 第一个查询的结果出现了两个 rock climbing 关键字, 匹配程度更高, 而第二个则只匹配了一次, 所以工具source排序它更后

短语搜索 (单独搜索词)

curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "match_phrase" : {
            "about" : "I love"
        }
    }
}'

高亮搜索

curl -i -XGET -H "Content-Type: application/json" 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : {
        "match_phrase" : {
            "about" : "I love"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}'

得出的结果集合

...
"highlight" : {
        "about" : [
            "<em>I</em> <em>love</em> to go rock climbing"
        ]
}
....

Comments are closed.