user1036651
user1036651

Reputation: 417

elasticsearch tokenizes facet terms

I am using elasticsearch to filter products by certain criteria on the category page. One option is to filter by the manufacturer. The user can choose the manufacturer name and gets a filtered resultset. My problem is that elasticsearch seems to tokenize the facet terms although the mapping is defined as "not_analyzed".

See the following example:

My mapping looks like this:

POST /products_test/product
{
    "mappings" : {
        "product":{
            "properties":{
                "manufacturer": {
                    "type" : "string",
                    "index" : "not_analyzed"
                }
            }
        }
    }
}

Here is my test data:

POST /products_test/product/1
{
    "id": 1,
    "manufacturer": "bra"
}

POST /products_test/product/2
{
    "id": 2,
    "manufacturer": "abracada bra"
}

If I execute the following query I get one hit but two terms for the facet:

POST /products_test/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
          "term": {
             "manufacturer": "abracada"
          }
         }
      }
   },
   "facets": {
      "f_manufacturer": {
         "terms": {
            "field": "manufacturer",
            "size": 30,
            "order": "term",
            "all_terms": false
         }
      }
   }
}

Result:

   "facets": {
      "f_manufacturer": {
         "_type": "terms",
         "missing": 0,
         "total": 2,
         "other": 0,
         "terms": [
            {
               "term": "abracada",
               "count": 1
            },
            {
               "term": "bra",
               "count": 1
            }
         ]
      }
   }

What I expected was to get a single facet term with "abracada bra" as value.

Upvotes: 1

Views: 209

Answers (1)

Alex Brasetvik
Alex Brasetvik

Reputation: 11744

Your mapping and the behavior you expect from it is correct.

There is most likely something else amiss here, so you should check whether you actually have the assumed mapping.

For example, your term filter ({"term": {"manufacturer": "abracada"}}) does not match "abracada bra".

Here is a runnable example you can play with: https://www.found.no/play/gist/cf4e908967e6512bc3b2

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/products_test" -d '{
    "settings": {},
    "mappings": {
        "product": {
            "properties": {
                "manufacturer": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"products_test","_type":"product","_id":1}}
{"manufacturer":"bra"}
{"index":{"_index":"products_test","_type":"product","_id":2}}
{"manufacturer":"abracada bra"}
{"index":{"_index":"products_test","_type":"product","_id":3}}
{"manufacturer":"abracada"}
'

# Do searches

# Note: The filter is for "abracada bra" and not "abracada"
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "manufacturer": "abracada bra"
                }
            }
        }
    },
    "facets": {
        "f_manufacturer": {
            "terms": {
                "all_terms": false,
                "field": "manufacturer",
                "order": "term",
                "size": 30
            }
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "manufacturer": "abracada"
                }
            }
        }
    },
    "facets": {
        "f_manufacturer": {
            "terms": {
                "all_terms": false,
                "field": "manufacturer",
                "order": "term",
                "size": 30
            }
        }
    }
}
'

Upvotes: 2

Related Questions