如果没有空格,elasticsearch搜索不起作用

时间:2016-01-01 03:44:55

标签: php elasticsearch elasticsearch-plugin

我正在使用elasticsearch和php实现。我有9 x 4卡车司机,19 x 40汽车座椅等产品名称

我正在搜索我的索引

$params = [
            'index' => 'myindex',
            'type' => 'products',
            'body' => [
                    'query' => [
                            'match' => [
                                    'name' => '9 x12'
                            ]
                    ],

            ]
    ];

所以这会返回名称中包含9 x 12的产品。但是,当我尝试通过名称'来搜索9x12时=> ' 9 x12',没有任何回复。我错过了什么谢谢

EDITED 我用它来填充索引

PUT /myindex
{
   "settings": {
      "number_of_shards": 5,
      "number_of_replicas": 0,
      "analysis": {
         "analyzer": {
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
   },
   "mappings": {
      "doc": {
         "properties": {
            "text_field": {
               "type": "string",
               "analyzer": "whitespace_analyzer"
            }
         }
      }
   }
}

我也尝试过以下设置,但它们都无法正常工作

PUT /myindex
{
"settings":{
  "analysis": {
    "analyzer": {
      "lowercasespaceanalyzer": {
        "type": "custom",
        "tokenizer": "whitespace",
        "filter": [
          "lowercase"
        ]
      }
    }
  }
},


"mappings": {
 "products" : {
  "properties" : {
    "title" : { "type" : "string", "analyzer" : "lowercasespaceanalyzer", "tokenizer": "lowercase", "search_analyzer":"whitespace", "filter": [
      "lowercase"
    ] }
  }
 }
}
}

更新::我的当前映射

我使用api调用来获取当前的映射。不确定这是否会有所帮助,但这里是

{
clipboards: {
mappings: {
products: {
properties: {
product: {
properties: {
bottomleft_png: {},
bottomright_png: {},
cost: {},
date_added: {},
date_available: {},
date_modified: {},
description: {},
ean: {},
height: {},
image: {},
isbn: {},
jan: {},
length: {},
length_class_id: {},
location: {},
manufacturer_id: {},
minimum: {},
model: {},
mpn: {},
options: {},
points: {},
price: {},
product_gallery: {},
product_id: {},
quantity: {},
shipping: {},
sku: {},
sort_order: {},
status: {},
stock_status_id: {},
subtract: {},
tax_class_id: {},
topleft_png: {},
topright_png: {},
upc: {},
viewed: {},
weight: {},
weight_class_id: {},
width: {}
}
}
}
}
}
}
}

示例文档

  "_index": "clipboards",
            "_type": "products",
            "_id": "100",
            "_score": 1,
            "_source": {
               "product": {
                  "product_id": "100",
                  "model": "9043",
                  "sku": "",
                  "upc": "",
                  "ean": "",
                  "jan": "",
                  "isbn": "",
                  "mpn": "",
                  "location": "",
                  "quantity": "67",
                  "stock_status_id": "8",
                  "image": "catalog/Clipboards/Clipboard_accessories/Bands/ISO-bands/iso-clipboard-bands-a29135.jpg",
                  "manufacturer_id": "13",
                  "shipping": "1",
                  "name": "9x4 truck"
                  "price": "4.9500",
                  "points": "360",
                  "tax_class_id": "9",
                  "date_available": "2013-09-08",
                  "weight": "0.05000000",
                  "weight_class_id": "5",
                  "length": "0.00000000",
                  "width": "0.00000000",
                  "height": "0.00000000",
                  "length_class_id": "3",
                  "subtract": "1",
                  "minimum": "1",
                  "sort_order": "1",
                  "status": "1",
                  "viewed": "585",
                  "date_added": "2015-04-07 02:04:21",
                  "date_modified": "2015-11-25 12:42:17",
                  "topleft_png": "",
                  "options": [
                     {
                        "product_option_value_id": "31",
                        "product_option_id": "232",
                        "product_id": "100",
                        "option_id": "17",
                        "option_value_id": "64",
                        "quantity": "100",
                        "subtract": "1",
                        "price": "0.0000",
                        "price_prefix": "+",
                        "points": "0",
                        "points_prefix": "+",
                        "weight": "0.00000000",
                        "weight_prefix": "+",
                        "option_name": "Black",

                        "main_option_heading_sort_order": "1",
                        "main_option_heading": "ISO Band Color"
                     },
                     {
                        "product_option_value_id": "32",
                        "product_option_id": "232",
                        "product_id": "100",
                        "option_id": "17",
                        "option_value_id": "65",
                        "quantity": "100",
                        "subtract": "1",
                        "price": "0.0000",
                        "price_prefix": "+",
                        "points": "0",
                        "points_prefix": "+",
                        "weight": "0.00000000",
                        "weight_prefix": "+",
                        "option_name": "Pink",

                        "main_option_heading_sort_order": "2",
                        "main_option_heading": "ISO Band Color"
                     },
                     {
                        "product_option_value_id": "33",
                        "product_option_id": "232",
                        "product_id": "100",
                        "option_id": "17",
                        "option_value_id": "66",
                        "quantity": "100",
                        "subtract": "1",
                        "price": "0.0000",
                        "price_prefix": "+",
                        "points": "0",
                        "points_prefix": "+",
                        "weight": "0.00000000",
                        "weight_prefix": "+",
                        "option_name": "Clear",

                        "main_option_heading_sort_order": "3",
                        "main_option_heading": "ISO Band Color"
                     }
                  ],
                  "product_gallery": []
               }
            }
         }

1 个答案:

答案 0 :(得分:1)

由于您刚刚开始使用ES,因此最重要的一点是要了解analysis在ES中的工作原理。 This是一个很好的起点。

您正在whitespace tokenizer使用lowercase filter,因此当您将 9 X 12 编入索引时,会在inverted index中存储三个令牌,即 9 x 12 。索引 9x12 时,只生成一个令牌,即 9x12 本身。现在,当您搜索 9 x12 时,ES会搜索 9 x12 ,因此无法找到文档索引为 9x12

修改

根据您的要求,我创建了以下索引

POST prod_index
{
  "settings": {
    "analysis": {
      "analyzer": {
         "vehicle_analyzer": {
          "char_filter": [
            "vehicle_extractor"
          ],
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      },
      "char_filter": {
        "vehicle_extractor": {
          "type": "pattern_replace",
          "pattern": "(?i)(\\d+)\\s*x\\s*(\\d+)",
          "replacement": "$1x$2"
        }
      }
    }
  },
  "mappings": {
    "your_type": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "vehicle_analyzer"
        }
      }
    }
  }
}

我正在使用pattern_replace char filter,它的工作是将数字x数字形式的字符串组合成单个标记,例如 9x 12 9 x 12 9 x12 将变为 9x12 。您可以使用analyze api查看vehicle_ analyzer的工作方式。

现在您的查询将正常运行。 9 x12将返回所有可能的组合,您将获得9x12 truck9 x 12 car等。现在,如果您要搜索9 x12 truck,请使用match_phrase代替{{1} }}

这有帮助吗?