dsxml2169 2016-01-01 03:44 采纳率: 0%
浏览 127

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

I am getting my feet wet with elasticsearch and php implementation. I have product names like 9 x 4 trucker, 19 x 40 car seat, etc etc

I am searching my index like this

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

            ]
    ];

so this returns my products that have 9 x 12 in their name. However when I try to search for 9x12 by doing 'name' => '9 x12', nothing gets returned. What am I missing. Thanks

EDITED I used this to populate index

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

I have also tried the settings below but they did not work either

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

UPDATE :: My Current Mapping

I used the api call to get the current mapping. Not sure if this would help but here it is

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

Sample document

  "_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条回答 默认 最新

  • dongxi1680 2016-01-01 05:23
    关注

    Since you are just starting with ES, one of the most important thing to understand is how analysis works in ES. This is a good starting point.

    You are using whitespace tokenizer with lowercase filter, so when you index 9 X 12, three tokens are stored in inverted index namely 9, x and 12. When you index 9x12 only one token is generated i.e 9x12 itself. Now when you search 9 x12, ES searches for either 9 or x12 and hence it is not able to find the document indexed as 9x12

    EDIT

    From your requirements I created following index

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

    I am using pattern_replace char filter, its job is to combine strings of the form digit x digit to single token e.g 9x 12, 9 x 12 and 9 x12 will become 9x12. You can use analyze api to see how vehicle_ analyzer is working.

    Now your query will work fine. 9 x12 will return all possible combo, you will get 9x12 truck, 9 x 12 car etc. Now if you want to search for 9 x12 truck use match_phrase instead of match.

    Does this help?

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?