(13) ElasticSearch match query

match the query word is known to exist, the field will be word operation, then the query. The term does not word, will field as a whole query. ( Note: Data from previous )

1, with the match query name is zhaoliu or zhaoming, and can identify two records, but with the term query does not come out, because the inverted index list does not "zhaoliu zhaoming". as follows:

GET /lib3/user/_search
{
  "query":{
    "match":{
      "name":"zhaoliu zhaoming"
    }
  }
}

The results are as follows:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9808292,
    "hits": [
      {
        "_index": "lib3",
        "_type": "user",
        "_id": "2",
        "_score": 0.9808292,
        "_source": {
          "name": ",
          "Zhaoming"address": "bei jing hai dian qu qing he zhen",
          "age": 20,
          "birthday": "1998-10-12",
          "interests": "xi huan hejiu,duanlian,changge"
        }
      },
      {
        "_index": "lib3",
        "_type": "user",
        "_id": "1",
        "_score": 0.6931472,
        "_source": {
          "name": "zhaoliu",
          "address": "hei long jiang sheng tie ling shi",
          "age": 50,
          "birthday": "1970-12-12",
          "interests": "xi huang hejiu,duanlian,lvyou"
        }
      }
    ]
  }
}

Replace the term

GET /lib3/user/_search
{
  "query":{
    "term":{
      "name":"zhaoliu zhaoming"
    }
  }
}

The results are as follows:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

2, age 20 queries

GET /lib3/user/_search
{
  "query":{
      "match":{"age":20
      }
    }
}

3, query all, match_all

GET /lib3/user/_search
{
  "query":{
    "match_all":{}
  }
}

4, a plurality of fields can be specified, multi_match. As follows: interests or in the name will contain lvyou search out.

GET /lib3/user/_search
{
  "query":{
    "multi_match":{
      "query":"lvyou","fields":["interests","name"]
    }
  }
}

4, phrase match match_phrase, ElasticSearch engine first analysis (analyze) the query string, construct the query phrase from the text after the analysis, which means you must match all word phrases, and ensure the relative position of each word unchanged. (Understood as an exact match). The first follows a query to the document can not, you can query to the second document.

GET /lib3/user/_search
{
    "query":{
        "match_phrase":{
            "interests":"duanlian,shuoxiangsheng"
        }
    }
}

GET /lib3/user/_search
{
    "query":{
        "match_phrase":{
            "interests":"duanlian,changge"
        }
    }
}

5, which fields, _source indicate the need.

  (5.1) queries interests are changge, the results show only address, name field:

GET /lib3/user/_search
{
    "_source":["address","name"],
    "query":{
        "match":{
            "interests":"changge"
        }
    }
}

  (5.2) to query all, the results show only the name, address field

GET /lib3/user/_search
{
    "query":{
        "match_all":{}
    },
    "_source":{
        "includes":["name","address"]
    }
}

  (5.3) to query all, the results do not include age, birthday field

GET /lib3/user/_search
{
    "query":{
        "match_all":{}
    },
    "_source":{
        "excludes":["age","birthday"]
    }
}

  (5.4) wildcard. All inquiries, addr field names to begin with, does not include the name, do not include fields that begin with bir

GET /lib3/user/_search
{
    "_source":{
        "includes":"addr*",
        "excludes":["name","bir*"]
    },
    "query":{
        "match_all":{}
    }
}

6, sorted. Use sort implements sorting: desc descending, asc ascending

GET /lib3/user/_search
{
    "query":{
        "match_all":{}
        },
        "sort":{
            "age":{"order":"desc"}
        }
}

7, prefix matching the query. Check out the name of the document begins with zhao

GET /lib3/user/_search
{
    "query":{
        "match_phrase_prefix":{
            "name":{
                "query":"zhao"
            }
        }
    }
}

8, the scope of the query range. Parameters: from, to, include_lower, include_upper, boost.

  include_lower: whether to include the left edge of the range, the default is true,

  include_upper: contains the right border of the range, the default is true

GET /lib3/user/_search
{
    "query":{
        "range":{
            "birthday":{
                "from":"1990-10-10",
                "to":"2018-05-01"
            }
        }
    }
}
GET /lib3/user/_search
{
    "query":{
        "range":{
            "age":{
                "from":20,
                "to":25,
                "include_lower":true,
                "include_upper":false
            }
        }
    }
}

8, wildcard queries. Wildcards * and? To query

  * Represents 0 or more characters
  ? Represent any one character

GET /lib3/user/_search
{
    "query":{
        "wildcard":{
            "name":"li?i"
        }
    }
}

9, fuzzy fuzzy queries.

  value: keyword query
  boost: the weight of the query, the default value is 1.0
  min_similarity: minimum similarity matching set, the default value is 0.5, for a string of
         0-1 (including 0 and 1); for values the value may be greater than 1; for the value of the date of type 1d, 1m, etc., 1d represents one day
  prefix_length: indicates the length of the common prefix distinguishing lexical items, the default is 0
  max_expansions: terms in the query may be expanded the number of default can be infinite

  Below, the value of name is zholiu can check out the name of the document is zhaoliu, the value of interests is chagge can check out the value of the interests of the document is chagge

GET /lib3/user/_search
{
    "query":{
        "fuzzy":{
            "name":"zholiu"
        }
    }
}
GET /lib3/user/_search
{
    "query":{
        "fuzzy":{
            "interests":{
                "value":"chagge"
            }
        }
    }
}

10, highlight the query: highlight, search out the documents, where the search keyword tagged, such as <em> changge </ em> , the following is an example.

GET /lib3/user/_search
{
    "query":{
        "match":{
            "interests":"changge"
        }
    },
    "highlight":{
        "fields":{
            "interests":{}
        }
    }
}

The results are as follows:

{
  "took": 83,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "lib3",
        "_type": "user",
        "_id": "3",
        "_score": 0.6931472,
        "_source": {
          "name": ",
          "lisi"address": "bei jing hai dian qu qing he zhen",
          "age": 23,
          "birthday": "1998-10-12",
          "interests": "xi huan hejiu,duanlian,changge"
        },
        "highlight": {
          "interests": [
            "xi huan hejiu,duanlian,<em>changge</em>"
          ]
        }
      },
      {
        "_index": "lib3",
        "_type": "user",
        "_id": "2",
        "_score": 0.47000363,
        "_source": {
          "name": "zhaoming",
          "address": ""hai dian qu qing jing bei of He zhen,
          "age": 20,
          "birthday": "1998-10-12",
          "interests": "xi huan hejiu,duanlian,changge"
        },
        "highlight": {
          "interests": [
            "xi huan hejiu,duanlian,<em>changge</em>"
          ]
        }
      },
      {
        "_index": "lib3",
        "_type": "user",
        "_id": "5",
        "_score": 0.47000363,
        "_source": {
          "name": "zhangsan",
          "address": "bei jing chao yang qu",
          "age": 29,
          "birthday": "1988-10-12",
          "interests": "xi huan tingyinyue,changge,tiaowu"
        },
        "highlight": {
          "interests": [
            "xi huan tingyinyue,<em>changge</em>,tiaowu"
          ]
        }
      }
    ]
  }
}

 

Guess you like

Origin www.cnblogs.com/javasl/p/11405391.html