How to search in elastic search with separated by space and without space How to search in elastic search with separated by space and without space elasticsearch elasticsearch

How to search in elastic search with separated by space and without space


As suggested by @David in the comment, you can use the shingles token filter to achieve your use-case.

Adding a working example, which clearly shows the higher score for docs conatining L1 L2 while L1 and L2 docs also coming in search results.

Index mapping

{    "settings": {        "analysis": {            "analyzer": {                "my_analyzer": {                    "type": "custom",                    "tokenizer": "whitespace",                    "filter": [                        "shingle"                    ]                }            }        }    },    "mappings": {        "properties": {            "name": {                "type": "text",                "analyzer": "my_analyzer"            }        }    }}

Index sample docs

{    "name" : "L1 L2"}{    "name" : "L1"}{    "name" : "L2"}

Search query

{    "query": {        "match": {            "name": "L1 L2"        }    }}

And search response

 "hits": [            {                "_index": "shingleside",                "_type": "_doc",                "_id": "1",                "_score": 1.0462961,                "_source": {                    "name": "L1 L2"                }            },            {                "_index": "shingleside",                "_type": "_doc",                "_id": "2",                "_score": 0.5619608,                "_source": {                    "name": "L1"                }            },            {                "_index": "shingleside",                "_type": "_doc",                "_id": "3",                "_score": 0.5619608,                "_source": {                    "name": "L2"                }            }        ]