elasticsearch标记方面术语



我正在使用elasticsearch根据类别页面上的某些条件筛选产品。一种选择是由制造商进行过滤。用户可以选择制造商名称并获得过滤后的结果集。我的问题是,尽管映射被定义为"未分析",但弹性搜索似乎将方面术语标记化了。

参见以下示例:

我的映射如下:

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

这是我的测试数据:

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

如果我执行以下查询,我会得到一个命中,但有两个方面的术语:

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
         }
      }
   }
}

结果:

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

我所期望的是得到一个以"abracada bra"为价值的单面术语。

您的映射和期望的行为是正确的。

这里很可能还有其他问题,所以你应该检查一下你是否真的有假设的映射。

例如,术语过滤器({"term": {"manufacturer": "abracada"}})与"abracada bra"不匹配。

下面是一个可运行的示例,您可以使用它: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
            }
        }
    }
}
'

最新更新