ElasticSearch a basic query (the English word)

Ado First prepare the data, let's add a few documents

PUT /lib3/user/1
{ 
  "name" : "zhaoliu",
  "address" :"hei long jiang sheng tie ling shi",
  "age" : 50,
  "birthday" : "1970-12-12",
  "interests": "xi buan hejiu, duanlian, lvyou"
}

PUT /lib3/user/2
{
  "name" :"zhaoming" ,
  "address" : "bei jing hai dian qu qing he zhen",
  "age" : 20,
  "birthday" : "1998-10-12" ,
  "interests": "xi huan hejiu, duanlian, changge"
}


PUT /lib3/user/3
{
  "name" : "lisi",
  "address" : "bei jing hai dian qu qing he zhen",
  "age" : 23,
  "birthday" : "1998-10-12" ,
  "interests": "xi huan hejiu,duanlian, changge"
}

PUT /lib3/user/4
{
  "name": "wangwu",
  "address" : "bei jing hai dian qu qing he zhen",
  "age": 26,
  "birthday" : "1995-10-12" ,
  "interests": "xi huan biancheng, tingyinyue, lvyou"
}

PUT /lib3/user/5
{
  "name" :"zhangsan",
  "address" : "bei jing chao yang qu",
  "age" : 29,
  "birthday" : "1988-10-12",
  "interests": "xi huan tingyinyue , changge , tiaowu"
}

Find lisi simple person by name basic information GET / lib3 / user / _search q = name:? Lisi

{
  "took" : 43,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

This max_score: dependent matching score (which is calculated according to an algorithm ElasticSearch)

 

Queries Interests interests like changge (singing) people, and the age of flashbacks row

GET /lib3/user/_search?q=interests:changge&sort=age:desc
{
  "took" : 124,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        },
        "sort" : [
          29
        ]
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        },
        "sort" : [
          23
        ]
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        },
        "sort" : [
          20
        ]
      }
    ]
  }
}

term and terms queries (Find zhaoliu this person's information)

term query will go to the inverted index bow | to find the exact term, it does not know the word's existence. Such a query for keyword, numeric DATE..
Term: query a field contains a keyword documentation
GET / lib3 / the User / _search /
{ "Query": { "Term": {Interests ":" Changge "} }}
Terms: a query field with a plurality of keywords bonded document
GET / ib3 / user / _search { "query": { "terms ': {" interests ": [" hejiu "," changge']}}

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


GET /lib3/user/_search
{
  "query": {
    "term": {
      "name": "zhaoliu"
    }
  }
}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      }
    ]
  }
}

 

Find hobbies interests of the people as hejiu changge information

GET /lib3/user/_search
{
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 56,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_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" : 1.0,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

Long as it contains a keyword query will be out of a total of four people have about hejiu changge either changge either or both hejiu

If I just want to take the first two personal use from: 0 (starts from the first document) size: 2 (Take 2 documents)

GET /lib3/user/_search
{
  "from":0,
  "size":2,
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 81,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      }
    ]
  }
}

The above query is no version number we want to get the version number, just add a version: true

GET /lib3/user/_search
{
  "version": true, 
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_version" : 1,
        "_score" : 1.0,
        "_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",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

 

 

 

 

 

Published 298 original articles · won praise 107 · Views 140,000 +

Guess you like

Origin blog.csdn.net/ywl470812087/article/details/104865525