2016-01-22 38 views
2

我正在尝试使用Elasticsearch提供一个建议功能。 关注此文章https://qbox.io/blog/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams获取有关字段Elasticsearch的建议

我现在的工作,但不是在同一句话中的两个单词。

我现在在ES的数据是。

{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8p4ZTfM-Ee45GnF5", 
    "_score": 1, 
    "_source": { 
     "title": "Making a dish", 
     "author": "Jim haunter" 
    } 
}, 
{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8jaZTfM-Ee45GnF4", 
    "_score": 1, 
    "_source": { 
     "title": "The big fish", 
     "author": "Jane Stewart" 
    } 
}, 
{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8clRTfM-Ee45GnF3", 
    "_score": 1, 
    "_source": { 
     "title": "The Hunter", 
     "author": "Jame Franco" 
    } 
} 

这里是映射和设置。

{"settings": { 
    "analysis": { 
    "filter": { 
     "nGram_filter": { 
      "type": "nGram", 
      "min_gram": 2, 
      "max_gram": 20, 
      "token_chars": [ 
       "letter", 
       "digit" 
      ] 
     } 
    }, 
    "analyzer": { 
     "nGram_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "lowercase", 
       "nGram_filter" 
      ] 
     }, 
     "whitespace_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "lowercase" 
      ] 
     } 
    } 
    } 
}, 
"mappings": { 
    "books": { 
    "_all": { 
     "index_analyzer": "nGram_analyzer", 
     "search_analyzer": "whitespace_analyzer" 
    }, 
    "properties": { 
     "title": { 
      "type": "string", 
      "index": "no" 
     }, 
     "author": { 
      "type": "string", 
      "index": "no" 
     } 
     } 
    } 
    } 
} 

这里是搜索

{ 
    "size": 10, 
    "query": { 
    "match": { 
    "_all": { 
     "query": "Hunter", 
     "operator": "and", 
     "fuzziness": 1 
    } 
    } 
} 
} 

,当我搜索“的”我得到 “大鱼”和 “猎人”。 但是,当我输入“The Hunt”时,我一无所获。 为了再次获得这本书,我需要输入“The Hunte”。 有什么建议吗? 任何帮助表示赞赏。

回答

1

从工作区域中删除"index": "no"。另外,由于我使用的是ES 2.x,我不得不用"analyzer"替换“index_analyzer”。因此,这里的映射:

PUT /test_index 
{ 
    "settings": { 
     "analysis": { 
     "filter": { 
      "nGram_filter": { 
       "type": "nGram", 
       "min_gram": 2, 
       "max_gram": 20, 
       "token_chars": [ 
        "letter", 
        "digit" 
       ] 
      } 
     }, 
     "analyzer": { 
      "nGram_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase", 
        "nGram_filter" 
       ] 
      }, 
      "whitespace_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase" 
       ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "books": { 
     "_all": { 
      "analyzer": "nGram_analyzer", 
      "search_analyzer": "whitespace_analyzer" 
     }, 
     "properties": { 
      "title": { 
       "type": "string" 
      }, 
      "author": { 
       "type": "string" 
      } 
     } 
     } 
    } 
} 

下面是一些代码我用来测试它:

http://sense.qbox.io/gist/0140ee0f5043f66e76cc3109a18d573c1d09280b

+0

我不为我工作,我想我使用ES 1.7.2。也许这是问题? – dontberude

+0

Oppps对不起,它似乎工作。非常感谢。 – dontberude