2013-05-29 87 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 
       } 
      } 
     } 
    } 
} 

映射解决我的问题