2013-05-29 89 views
0

我有一個查詢像下面,Elasticsearch搜索查詢只返回精確匹配

任何想法,爲什麼我只得到確切的比賽結果時,我做搜索。例如 ;

當我搜索「的Aegli」我得到的結果,但是當我搜索「Aegl」未返回任何結果

query = { 
    "query": { 
    "query_string": { 
     "query": "%s"%q 
    } 
    }, 
    "filter": { 
    "term": { 
     "has_product": 1 
    } 
    }, 
    "facets": { 
    "destination": { 
     "terms": { 
     "field": "destination.en" 
     }, 
     "facet_filter": { 
     "term": { 
      "has_product": 1 
     } 
     } 
    }, 
    "hotel_class": { 
     "terms": { 
     "field": "hotel_class" 
     }, 
     "facet_filter": { 
     "term": { 
      "has_product": 1 
     } 
     } 
    }, 
    "hotel_type": { 
     "terms": { 
     "field": "hotel_type" 
     }, 
     "facet_filter": { 
     "term": { 
      "has_product": 1 
     } 
     } 
    } 
    } 
} 
+0

這就像Lucene的倒排索引應該工作的方式。您可以決定索引ngrams(但索引將快速增長)或使用通配符查詢(較慢)。 – javanna

回答

0

我沒有看到你真正的查詢,但您可能在搜索結束時丟失*字和你的查詢字符串應該是這樣的;

{"query_string": {"query": "%s*"} 

例如;

{"query_string": {"query": "Aegl*"} 
+0

我認爲這應該用NGRAM標記器來解決。這種方法可能會縮小比例。 – tunaktunak

+1

你可以在這裏看看關於標記物的討論http://stackoverflow.com/questions/16816628/fetch-all-documents-if-source-contains-the-given-search-text-in-elastic-search-小號 – jackdbernier

0

具有類似於下面

{ 
    "mappings": { 
     "hotel": { 
      'properties': {"name": { 
       "type": "string", 
       "search_analyzer": "str_search_analyzer", 
       "index_analyzer": "str_index_analyzer" 
      } 
      }}, 

    }, 

    "settings": { 
     "analysis": { 
      "analyzer": { 
       "str_search_analyzer": { 
        "tokenizer": "keyword", 
        "filter": ["lowercase"] 
       }, 

       "str_index_analyzer": { 
        "tokenizer": "keyword", 
        "filter": ["lowercase", "substring"] 
       } 
      }, 

      "filter": { 
       "substring": { 
        "type": "nGram", 
        "min_gram": 1, 
        "max_gram": 20 
       } 
      } 
     } 
    } 
} 

映射解決我的問題