The MATCH clause allows to perform full-text searches in
text fields. The query string at input is tokenized
using same settings applied to the text during indexing. In addition to
tokenization of input text, the query string supports a number of full-text
operators that allow enforcing different rules on how keywords
should provide a valid match.
The full-text match clauses can be combined with attribute filters as an AND boolean. OR relation between full-text matches and attribute filters are not supported.
The match query is always executed first in the filtering process, followed by the attribute filters. The attribute filters are applied on the result set of the match query. A query without a match clause is called a fullscan.
There must be at most one MATCH() in the
SELECT clause.
Using the full-text query syntax matching is performed over all indexed text fields of a document, unless the expression requires to be match within a field (like phrase search) or limited by field operators.
SELECT * FROM index WHERE MATCH('cats|birds');SELECT statement uses MATCH clause for performing full-text searches. It accepts an input string in which all full-text operators are available.
SELECT * FROM myindex WHERE MATCH('"find me fast"/2');+------+------+----------------+
| id | gid | title |
+------+------+----------------+
| 1 | 11 | first find me |
| 2 | 12 | second find me |
+------+------+----------------+
2 rows in set (0.00 sec)
Full-text matching is available in the /search endpoint
and in HTTP-based clients. The following clauses can be used for
performing full-text matches:
“match” is a simple query that matches the specified keywords in the specified fields
"query":
{
"match": { "field": "keyword" }
}You can specify a list of fields:
"match":
{
"field1,field2": "keyword"
}Or you can use _all or * to search all
fields.
You can search all fields except one using “!field”:
"match":
{
"!field1": "keyword"
}By default keywords are combined using the OR operator. However, you can change that behaviour using the “operator” clause:
"query":
{
"match":
{
"content,title":
{
"query":"keyword",
"operator":"or"
}
}
}“operator” can be set to “or” or “and”.
“match_phrase” is a query that matches the entire phrase. It is similar to a phrase operator in SQL. Here’s an example:
"query":
{
"match_phrase": { "_all" : "had grown quite" }
}“query_string” accepts an input string as a full-text query in
MATCH() syntax
"query":
{
"query_string": "Church NOTNEAR/3 street"
}All full-text match clauses can be combined with must, must_not and should operators of an HTTP bool
query.
Examples:
POST /search
-d
'{ "index" : "myindex",
"query":
{
"match":
{
"*" : "find me"
}
}
}'{
"took":10,
"timed_out": false,
"hits":
{
"total": 2,
"hits":
[
{
"_id": "1",
"_score": 1,
"_source": {"title":"first find me fast", "gid": 11 }
},
{
"_id": "2",
"_score": 1,
"_source": { "title":"second find me fast", "gid": 12 }
}
]
}
}POST /search
-d
'{ "index" : "myindex",
"query":
{
"match_phrase":
{
"*" : "find me"
}
}
}'{
"took":10,
"timed_out": false,
"hits":
{
"total": 2,
"hits":
[
{
"_id": "1",
"_score": 1,
"_source": {"title":"first find me fast", "gid": 11 }
},
{
"_id": "2",
"_score": 1,
"_source": { "title":"second find me fast", "gid": 12 }
}
]
}
}POST /search
-d
'{ "index" : "myindex",
"query":
{
"query_string": "@title \"find me fast \"/2"
}
}'{
"took":10,
"timed_out": false,
"hits":
{
"total": 2,
"hits":
[
{
"_id": "1",
"_score": 1,
"_source": {"title":"first find me fast", "gid": 11 }
},
{
"_id": "2",
"_score": 1,
"_source": { "title":"second find me fast", "gid": 12 }
}
]
}
}$search = new Search(new Client());
$result = $search->('@title find me fast');
foreach($result as $doc)
{
echo 'Document: '.$doc->getId();
foreach($doc->getData() as $field=>$value)
{
echo $field.': '.$value;
}
}Document: 1
title: first find me fast
gid: 11
Document: 2
title: second find me fast
gid: 12Python
searchApi.search({"index":"myindex","query":{"query_string":"@title \"find me fast \"/2"}}){'hits': {'hits': [{u'_id': u'1',
u'_score': 1,
u'_source': {u'title': u'first find me fast', u'gid':11}},
{u'_id': u'2',
u'_score': 1,
u'_source': {u'title': u'second find me fast', u'gid':12}}],
'total': 2},
'profile': None,
'timed_out': False,
'took': 0}javascript
res = await searchApi.search({"index":"myindex","query":{"query_string":"@title \"find me fast \"/2"}});{"hits": {"hits": [{"_id": "1",
"_score": 1,
"_source": {"title": "first find me fast", "gid":11}},
{"_id": "2",
"_score": 1,
"_source": {"title": "second find me fast", "gid":12}}],
"total": 2},
"profile": None,
"timed_out": False,
"took": 0}java
query = new HashMap<String,Object>();
query.put("query_string","@title \"find me fast \"/2");
searchRequest = new SearchRequest();
searchRequest.setIndex("forum");
searchRequest.setQuery(query);
searchResponse = searchApi.search(searchRequest);class SearchResponse {
took: 0
timedOut: false
hits: class SearchResponseHits {
total: 2
hits: [{_id=1, _score=1, _source={title=first find me fast, gid=11}}, {_id=2, _score=1, _source={title=first find me fast, gid=12}}]
aggregations: null
}
profile: null
}