Skip to content

Instantly share code, notes, and snippets.

@dainiusjocas
Created February 24, 2021 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainiusjocas/c9a2bca9c3870c4c23592d56b7abba2f to your computer and use it in GitHub Desktop.
Save dainiusjocas/c9a2bca9c3870c4c23592d56b7abba2f to your computer and use it in GitHub Desktop.
Phrase query examples when implemented as shingles
DELETE /phrases-shingle-example
PUT /phrases-shingle-example
{
"settings": {
"analysis": {
"filter": {
"my_shingle_filter": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 2,
"output_unigrams": false
}
},
"analyzer": {
"rebuilt_shingle": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding",
"my_shingle_filter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "standard",
"fields": {
"shingle": {
"type": "text",
"analyzer": "rebuilt_shingle"
}
}
}
}
}
}
# Have a look at the tokens after analysis
POST phrases-shingle-example/_analyze
{
"field": "title.shingle",
"text": ["Raudona gėlėta suknelė"]
}
POST /phrases-shingle-example/_doc/1
{"title":"Raudona gėlėta suknelė"}
POST /phrases-shingle-example/_doc/2
{"title":"Gėlėta raudona suknelė"}
POST /phrases-shingle-example/_doc/3
{"title":"Raudona suknelė"}
POST /phrases-shingle-example/_doc/4
{"title":"Raudonos suknelės"}
GET phrases-shingle-example/_refresh
# two hits
GET /phrases-shingle-example/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title.shingle": {
"query": "raudona suknele",
"_name": "phrase"
}
}
}
]
}
}
}
# The same two hits despite the fact that "neriniuota" is mentioned
GET /phrases-shingle-example/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title.shingle": {
"query": "raudona suknele neriniuota",
"_name": "phrase"
}
}
}
]
}
}
}
# No hits at all because of "neriniuota" and "operator and",
# which enforces that all tokens from the query must be present in the document
GET /phrases-shingle-example/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title.shingle": {
"query": "raudona suknele neriniuota",
"_name": "phrase",
"operator": "and"
}
}
}
]
}
}
}
# No hits at all because of "neriniuota" and minimum should match
# as an alternative to "operator and"
GET /phrases-shingle-example/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title.shingle": {
"query": "raudona suknele neriniuota",
"_name": "phrase",
"minimum_should_match": "100%"
}
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment