1

這裏是我的領域上elasticSearch:ElasticSearch分析

"keywordName": { 
     "type": "text", 
     "analyzer": "custom_stop" 
     } 

這裏是我的分析:

"custom_stop": { 
     "type":  "custom", 
     "tokenizer": "standard", 
     "filter": [ 
     "my_stop", 
     "my_snow", 
     "asciifolding" 
     ] 
    } 

這裏是我的過濾器:

  "my_stop": { 
       "type":  "stop", 
       "stopwords": "_french_" 
      }, 
      "my_snow" : { 
       "type" : "snowball", 
       "language" : "French" 
      } 

這裏是我的記錄我的索引(僅在我的字段中:keywordName):

「canne a peche」,「canne」,「canne a peche telescopique」,「iphone 8」,「iphone 8 case」,「iphone 8 cover」,「iphone 8 charger」,「iphone 8 new」

當我搜索「CANNE」,它給我的「CANNE」的文件,這就是我想要的:

GET ads/_search 
{ 
    "query": { 
    "match": { 
     "keywordName": { 
     "query": "canne", 
     "operator": "and" 
     } 
    } 
    }, 
    "size": 1 
} 

當我搜索「CANNEàPÊCHE」,它給了我「CANNE一個PECHE」這也可以。 「CannesàPêche」 - >「canne a peche」 - >確定。

這裏有一個棘手的部分:當我搜索「iPhone 8」時,它給了我「iPhone 8的覆蓋」,而不是「iPhone 8」。如果我改變大小,我設置5(因爲它返回包含「iphone 8」的5個結果)。我看到「iphone 8」是第四項成績。首先是「iphone 8套」,然後「iphone 8案」,然後「iphone 8個新」,最後是「iphone 8」 ......

下面是該查詢的結果:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 5, 
    "max_score": 1.4009607, 
    "hits": [ 
     { 
     "_index": "ads", 
     "_type": "keyword", 
     "_id": "iphone 8 cover", 
     "_score": 1.4009607, 
     "_source": { 
      "keywordName": "iphone 8 cover" 
     } 
     }, 
     { 
     "_index": "ads", 
     "_type": "keyword", 
     "_id": "iphone 8 case", 
     "_score": 1.4009607, 
     "_source": { 
      "keywordName": "iphone 8 case" 
     } 
     }, 
     { 
     "_index": "ads", 
     "_type": "keyword", 
     "_id": "iphone 8 new", 
     "_score": 0.70293105, 
     "_source": { 
      "keywordName": "iphone 8 new" 
     } 
     }, 
     { 
     "_index": "ads", 
     "_type": "keyword", 
     "_id": "iphone 8", 
     "_score": 0.5804671, 
     "_source": { 
      "keywordName": "iphone 8" 
     } 
     }, 
     { 
     "_index": "ads", 
     "_type": "keyword", 
     "_id": "iphone 8 charge", 
     "_score": 0.46705723, 
     "_source": { 
      "keywordName": "iphone 8 charge" 
     } 
     } 
    ] 
    } 
} 

哪有我保持關鍵字「canne a peche」(重音,大寫字母,複數項)的靈活性,但也告訴他如果有完全匹配(「iphone 8」=「iphone 8」),請給我確切的關鍵字名稱?

回答

1

我建議是這樣的:

"keywordName": { 
     "type": "text", 
     "analyzer": "custom_stop", 
     "fields": { 
     "raw": { 
      "type": "keyword" 
     } 
     } 
    } 

和查詢:

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "match": { 
      "keywordName": { 
       "query": "iphone 8", 
       "operator": "and" 
      } 
      } 
     }, 
     { 
      "term": { 
      "keywordName.raw": { 
       "value": "iphone 8" 
      } 
      } 
     } 
     ] 
    } 
    }, 
    "size": 10 
} 
+0

這是我一直在尋找的行爲! Thx – Gun

+0

是否有可能提高「最匹配」的結果?我的意思是 - >如果我搜索「samsung」,則有1個標記:「samsung」。但最好的分數是「三星銀河」(1.11),然後是「三星充電器」(0.94)和「三星」(0.84)。我怎麼能告訴它提升「三星」,因爲它與「sâmsung」最接近?而不是「三星Galaxy」或「三星充電器」 – Gun

1

匹配查詢使用tf/idf算法。這意味着你會得到按頻率排序的模糊搜索結果。如果你想在一個完全匹配的情況下得到結果,你應該在之前創建一個query_string的情況,如果沒有結果使用你的匹配查詢。

+0

所以我必須使用2個查詢來滿足我的期望? – Gun

+0

這就是我的看法。我也不明白爲什麼iPhone 8是第四個結果。 tf/idf應該給它最高的頻率。 – RoiHatam

+0

@Gun你可以顯示你的搜索結果size = 5 – RoiHatam