2016-01-01 79 views
0

我正在用elasticsearch和php實現我的腳。我有產品名稱,如9×4卡車司機,19×40汽車座椅,等等等等如果沒有空格,elasticsearch搜索不起作用

我正在尋找這樣的

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

      ] 
    ]; 

我的索引,以便該返回自己的產品,在他們的名字有9×12。但是當我嘗試通過'name'=>'9 x12'來搜索9x12時,沒有任何東西會被返回。我錯過了什麼。由於

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": [] 
       } 
      } 
     } 
+0

也映射看起來不正確,你能不能跑捲曲-XGET'的'localhost:9200/your_index/your_type/_mapping''和發佈的,輸出? – ChintanShah25

+0

如果您更清楚地陳述您的要求,哪些文件應該符合'9 x12',這將有所幫助? – ChintanShah25

+0

不知道你的文件是什麼意思?我需要在名稱字段和產品類型上進行搜索! – Autolycus

回答

1

因爲你只是用ES,最重要的事情之一開始瞭解如何analysis在ES工程。 This是一個很好的起點。

您正在使用whitespace tokenizerlowercase filter,所以當你的索引9×12,三個令牌存儲在inverted index即,X和。當指數9x12只有一個令牌生成即9x12本身。現在,當你搜索9 X12,ES搜索要麼 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" 
     } 
     } 
    } 
    } 
} 

我是usin克pattern_replace char filter,它的工作是形式數字乘以數字的字符串相結合,單令牌e.g 9X 129×129 X12將變得9x12。您可以使用analyze api來查看vehicle_ analyzer的工作方式。現在

您的查詢將正常工作。9 x12將返回所有可能的組合,您將獲得9x12 truck,9 x 12 car等。現在,如果要搜索9 x12 truck使用match_phrase而不是match

這有幫助嗎?

+0

嗨,謝謝。我嘗試過,但不幸的是,它沒有返回任何結果9 x 12或9x12或其他任何東西。我沒有得到任何回報。下面是我使用 – Autolycus

+0

查詢{ 「查詢」:{ 「匹配」:{ 「名」:「9x12」 } } } – Autolycus

+0

它的工作對我來說,我昨天測試了它,你正在使用我寫的同一個分析器? – ChintanShah25