This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

If ❤️ my article is helpful, welcome to like, follow. This is the greatest encouragement for me to continue my technical creation. More past articles in my personal column

ElasticSearch URI Search example

The query in Elasticsearch uses a DSL(query specific language) that you’ve probably all used. In addition, Elasticsearch can also be specified by URI.

It’s not as powerful as DSL, but it’s worth learning more.

The basic query

The story begins with a query:

GET /movies/_search? q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s

As you can see above, it contains the following parameters:

  • Q Specifies the query statement, using query String syntax
  • Df Indicates the default field. It is not specified. Yes All fields are queried
  • Sort Sort/from and size are used for paging
  • Profiles can view how queries are being executed

Query Field Syntax

  • {“profile”:”true”} Displays the query execution plan
  • Specify field query vs generic query: q=title:2012 / q=2012
  • Term vs Phrase
    • Beautiful Mind is equivalent to an OR query
    • “Beautiful Mind” is equivalent to and query
    • Phrase Must be stored in the same sequence
  • Grouping and quotation marks
    • Title :(Beautiful Mind) group means must appear together
    • Title =”Beautiful Mind” Phrase
  • Boolean operations
    • AND/OR/NOT OR && / | | /!
      • Must be capitalized
  • grouping
      • Said must
      • Said must_not
    • Title :(+ matrix-reloaded) must have a matrix, and (because it is a group) cannot have a reloaded
  • Range queries
    • Interval: [] closed interval,{} open interval
      • year:>=1980
      • year:{2019 TO 2018}
      • year:[* TO 2019]
  • Math symbols
    • year:>2010
    • year:(>2010 && <=2018)
    • year:(+>2010 +<=2018>)
  • Wildcard (Do not use it because the query efficiency is low and the memory usage is large. Especially in the front)
    • ? Represents a character, and * represents 0 or more characters
  • Regular expression
    • title:[bt]oy
  • Fuzzy matching and approximate query
    • title:befutifl~1
    • title:”lord rings”~2

Specific operation

GET /movies/_search? Q =2012&df=title&sort=year:desc&from=0&size=10&timeout=1s Q = 2012 & df = title {" profile ":" true "} = = = = = = = = = = = = = = = = = = return result = = = = = = = = = = = = = = = = = = {" took ": 76," timed_out ": false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 4.1047716, "hits" : [{" _index ":" tag ", "_type" : "_doc", "_id" : "hFpZH3sBgpUFltoIW7vi", "_score" : 4.1047716, "_source" : {"memberId" : 618, "phone" : "15321761517", "sex" : "1", "channel" : 4, "subOpenId" : "gcik-4nwr9lsy7pv", "address" : 618, "regTime" : "2019-11-22", "orderCount" : 3, "orderTime" : "FavGoods" : [2, 11, 16], "couponTimes" : [" 2019-11-25 ", "2019-11-27", "2019-11-13"], "chargeMoney" : 25.0, "overTime" : 2100}}}], "profile" : {" shards ": [ { "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][0]", "searches" : [ { "query" : [ { "type" : "TermQuery", "description" : "subOpenId:gcik", "time_in_nanos" : 237449, "breakdown" : { "score" : 0, "build_scorer_count" : 8, "match_count" : 0, "create_weight" : 231939, "next_doc" : 0, "match" : 0, "create_weight_count" : 1, "next_doc_count" : 0, "score_count" : 0, "build_scorer" : 5501, "advance" : 0, "advance_count" : 0 } } ], "rewrite_time" : 3399, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 81522, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 64422 } ] } ] } ], "aggregations" : [ ] }, { "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][1]", "searches" : [ { "query" : [ ...... ], "rewrite_time" : 3546, "collector" : [ ...... ] } ], "aggregations" : [ ] }, { "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][2]", "searches" : [ { "query" : [ ...... } ], "rewrite_time" : 4308, "collector" : [ ...... ] } ], "aggregations" : [ ] }, { "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][3]", "searches" : [ { "query" : [ ...... ], "rewrite_time" : 3412, "collector" : [ ...... ] } ], "aggregations" : [ ] }, { "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][4]", "searches" : [ { "query" : [ ...... ], "rewrite_time" : 22285, "collector" : [ ...... ] } ] } ], "aggregations" : [ ] } ] } }Copy the code

Other URI queries

# Generic query, right on _all, all fieldsGET /movies/_search? q=2012 {"profile":"true"
}

# Specify fieldGET /movies/_search? q=title:2012&sort=year:desc&from=0&size=10&timeout=1s {"profile":"true"
}


Find a beautiful Mind. Mind is a generic queryGET /movies/_search? q=title:Beautiful Mind {"profile":"true"
}

# generic queryGET /movies/_search? q=title:2012 {"profile":"true"
}

Use quotation marks, Phrase to queryGET /movies/_search? q=title:"Beautiful Mind"
{
	"profile":"true"
}

# Group, Bool queryGET /movies/_search? q=title:(Beautiful Mind) {"profile":"true"
}


# Boolean operator
# Find a beautiful MindGET /movies/_search? q=title:(Beautiful AND Mind) {"profile":"true"
}

# Find a beautiful MindGET /movies/_search? q=title:(Beautiful NOT Mind) {"profile":"true"
}

# Find a beautiful MindGET /movies/_search? q=title:(Beautiful %2BMind) {"profile":"true"
}


# Range queryGET /movies/_search? q=title:beautiful AND year:[2002 TO 2018%7D {"profile":"true"
}


# Wildcard queryGET /movies/_search? q=title:b* {"profile":"true"
}

# Fuzzy matching & Approximate matchingGET /movies/_search? q=title:beautifl~1 {"profile":"true"} GET /movies/_search? q=title:"Lord Rings"~ 2 {"profile":"true"
}


Copy the code

reading

  • www.elastic.co/guide/en/el…
  • www.elastic.co/guide/en/el…