ElasticSearch Introduction (two) - Simple Query

Return all records

Using the GET method, direct request / Index / _search, returns all records.

GET /accounts/_search
{
    "took": 683,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "Accounts" ,
                "_type" : "Person" ,
                "the _id" : ". 1" ,
                "_score" : 1.0 ,
                "_Source" : {
                    "User" : "
Joe Smith",
                    "title":"
Engineer",
                    " desc ":"
database management, software development"
                }
            }
        ]
    }
}

In the above code, the returned results took field indicates the time-consuming operation (in milliseconds), TIMED_OUT field indicates whether a timeout, Hits field indicates the record hit, meaning inside the sub-fields are as follows.

  • total: Returns the number of records, the present embodiment is two.
  • max_score: the highest matching degree, the present embodiment is 1.0.
  • hits: returns an array of records.

Returned records, each record has a field _score, the program indicates a match, according to this default field in descending order.

research all

Elastic very particular query, use your own query syntax requires GET request with the data volume.

GET /accounts/_search
{
    "query": {
        "match": {
            "desc": "
软件"
        }
    }
}

The above code uses the  Match query , matching the specified condition is desc field which contains the "software" of the word. Returned the following results.

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "accounts",
                "_Type" : "Person" ,
                "the _id" : ". 1" ,
                "_score" : 0.5753642 ,
                "_Source" : {
                    "User" : "
Joe Smith
",
                    "title":"
Engineer",
                    "desc":"
database management, software development"
                }
            }
        ]
    }
}

Elastic return a default 10 results, this setting can be changed by the size field.

GET /person/_search
{
    "query": {
        "match": {
            "desc": "
管理
"
        }
    },
    "size": 1
}

上面代码指定,每次只返回一条结果。

还可以通过from字段,指定位移。

GET /accounts/_search
{
    "query": {
        "match": {
            "desc": "
管理
"
        }
    },
    "from": 1,
    "size": 1
}

上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

逻辑运算

如果有多个搜索关键字, Elastic 认为它们是or关系。

GET /accounts/_search
{
    "query": {
        "match": {
            "desc": "
软件 系统"
        }
    },
    "from": 0,
    "size": 1
}

上面代码搜索的是软件 or 系统。

如果要执行多个关键词的and搜索,必须使用布尔查询

GET /accounts/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                        "user": "
张三"
                    }
                },
                {
                    "match": {
                        "desc": "
数据库"
                    }
                }
            ]
        }
    }
}

Guess you like

Origin www.cnblogs.com/TianFang/p/11330230.html