python elasticsearch 入门教程(三)全文短语搜索

from elasticsearch import Elasticsearch
es = Elasticsearch()

phrase={
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
rt2= es.search(index="megacorp", body=phrase)
print(rt2)
{'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5},
 'hits': {'hits': [{'_id': '1',
    '_index': 'megacorp',
    '_score': 0.5753642,
    '_source': {'about': 'I love to go rock climbing',
     'age': 25,
     'first_name': 'John',
     'interests': ['sports', 'music'],
     'last_name': 'Smith'},
    '_type': 'employee'}],
  'max_score': 0.5753642,
  'total': 1},
 'timed_out': False,
 'took': 278}


找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的雇员记录。

posted @ 2018-08-07 11:27  luoganttcc  阅读(157)  评论(0)    收藏  举报