0
votes

I have ten or so fields in all my documents: One in particular is product_code which is unique per document and of type string.

I have a match query on _all that works well, but I would like to perform a "fuzzy match" while preserving the ability to search for exact product_code

Here's what I've attempted:

"query": {
    "bool": {
        "should": [
            {
                "match": {
                    "product_code": {
                        "query": searchString,
                        "operator": "AND"
                    }
                }
            },
            {
                "match": {
                    "_all": {
                        "query": searchString,
                        "operator": "AND"
                        "fuzziness": 2,
                        "prefix_length": 2
                    }
                }
            }
        ]
    }
}

The problem with this approach is that the fuzziness is being applied to searches for product_code as well because it's included in _all.

Is there a way to either perform the search on product_code first and if no results are found, perform the search on _all, or exclude product_code from the _all query?

Any help is greatly appreciated.

1

1 Answers

1
votes

yes you can exlude product_code from _all using the following mappings.

PUT index_name
{
    "settings": {
        "analysis": {
            "analyzer": {},
            "filter": {}
        }
    },
    "mappings": {
        "type_name": {
            "properties": {
                "product_code": {
                    "type": "string",
                    "include_in_all": false
                }
            }
        }
    }
}

Alternatively you can use query_string search which also offer fuzziness.

Use the following query which use query string with AND operator and fuzziness settings

{
    "query": {
        "bool": {
            "should": [{
                "query_string": {
                    "fields": ["product_code", "other_field"],
                    "query": "this is my string",
                    "default_operator": "AND",
                    "fuzziness": 2,
                    "fuzzy_prefix_length": 2
                }
            }, {
                "match": {
                    "product_code": {
                        "query": "this is my string",
                        "operator": "AND"
                    }
                }
            }]
        }
    }
}

Hope this helps