2017-04-19 72 views
0

我有一個帶有產品的彈性搜索5.3服務器。 每個產品都有一個14位數的產品代碼,必須按照以下規則進行搜索。完整的代碼應該與僅包含最後9位數字,最後6位數字,最後5位數字或最後4位數字的搜索字詞匹配。使用自定義分析器在elasticsearch中獲取multi_match cross_fields查詢的結果

爲了達到這個目的,我創建了一個自定義分析器,它使用模式捕獲標記過濾器在索引時創建適當的標記。這似乎工作正常。 _analyse API顯示正確的術語被創建。

從彈性搜索中獲取文檔我正在使用multi_match cross_fields bool查詢來同時搜索多個字段。

當我有一個匹配產品代碼的部分查詢字符串和匹配任何其他字段的部分時,不會返回任何結果,但是當我分別搜索每個部分時,會返回相應的結果。另外,當我有多個部分跨越除產品代碼之外的任何字段時,都會返回正確的結果。

我的馬坪和分析:

PUT /store 
{ 
    "mappings": { 
     "products":{ 
      "properties":{ 
       "productCode":{ 
        "analyzer": "ProductCode", 
        "search_analyzer": "standard", 
        "type": "text" 
       }, 
       "description": { 
        "type": "text" 
       }, 
       "remarks": { 
        "type": "text" 
       } 
      } 
     } 
    }, 
    "settings": { 
     "analysis": { 
      "filter": { 
       "ProductCodeNGram": { 
        "type": "pattern_capture", 
        "preserve_original": "true", 
        "patterns": [ 
         "\\d{5}(\\d{9})", 
         "\\d{8}(\\d{6})", 
         "\\d{9}(\\d{5})", 
         "\\d{10}(\\d{4})" 
        ] 
       } 
      }, 
      "analyzer": { 
       "ProductCode": { 
        "filter": ["ProductCodeNGram"], 
        "type": "custom", 
        "preserve_original": "true", 
        "tokenizer": "standard" 
       } 
      } 
     } 
    } 
} 

查詢

GET /store/products/_search 
{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "multi_match": { 
         "query": "[query_string]", 
         "fields": ["productCode", "description", "remarks"], 
         "type": "cross_fields", 
         "operator": "and" 
        } 
       } 
      ] 
     } 
    } 
} 

的樣本數據

POST /store/products 
{ 
    "productCode": "999999123456789", 
    "description": "Foo bar", 
    "remarks": "Foobar" 
} 

的FOLL由於查詢字符串全部返回一個結果:

「456789」,「foo」,「foobar」,「foo foobar」。

但query_string「foo 456789」不返回任何結果。

我很好奇爲什麼最後的搜索沒有返回任何結果。我確信它應該。

回答

相關問題