Examples of use of the python es (record)

from elasticsearch Import elasticsearch 

esUrl = ' HTTP: // localhost: 9200 ' 

es = elasticsearch (esUrl) 

index = ' the Users ' 

# Create an index 

IF (es.indices.exists (index) == False): 
    Mapping = {
         ' Dynamic ' : '' , # automatically create indexes 
        ' the Properties ' : {
             ' title ' : {
                 ' of the type ' :' Text ' ,
                 ' analyzer ' : ' ik_max_word ' ,
                 ' search_analyzer ' : ' ik_max_word " 
            }, 
            " url " : {
                 " type " : " string " 
            }, 
            " date " : {
                 " type " : " date "
            }
        }
    }
    the Result=es.indices.create (index) 
    es.indices.analyze (index, body = Mapping) 

DATAS = [ 
    { 
        ' title ' : ' US to leave Iraq is a mess right ' ,
         ' url ' : ' HTTP: // View. news.qq.com/zt2011/usa_iraq/index.htm ' ,
         ' DATE ' : ' 2011-12-16 ' , 
    }, { 
        ' title ' : ' Ministry of Public Security: around the school bus to enjoy the highest right of way ' ,
         ' url ' :'http://www.chinanews.com/gn/2011/12-16/3536077.shtml ' ,
         ' DATE ' : ' 2011-12-16 ' , 
    }, { 
        ' title ' : ' Korea fishing alarm conflict investigation: per day police Han Chinese fishing buckle 1 ' ,
         ' URL ' : ' https://news.qq.com/a/20111216/001044.htm ' ,
         ' DATE ' : ' 2011-12-17 ' , 
    }, { 
        ' title ' : ' Chinese Consulate in Los Angeles shooting suspects have been Asian man surrendered ',
        'url': 'http://news.ifeng.com/world/detail_2011_12/16/11372558_0.shtml',
        'date': '2011-12-18',
    }
]

for k, row in enumerate(datas):
    es.index(index, body=row, doc_type='user', id=(k + 1))

search = {
    'query': {
        'match': {
            'title': ' Across the shared bus ' 
        } 
    }, 
    ' highlight ' : {
         ' Fields ' : {
             ' title ' : {} 
        } 
    } 
} 

'' ' customized highlight 

the following parameters can be changed returned results. Separate fields that can be set different parameters may be defined as uniform in the highlight property. 

number_of_fragments 
        the fragment refers to the period of continuous text. Return result can contain paragraphs discontinuous text. The default is 5. 

fragment_size 
       how many characters some fragment contains. Default 100. 

pre_tags 
       mark highlight start tag. The above example <em>. 

post_tags 
       mark the end of the highlight label. For example, above </ em>. 

encoder 
       explain whether the field is html format, default: not, html: Yes. 

no_match_size
       Even if the field does not hit the key, you can return a text, which is returned from the start parameter indicates how many characters. '' ' 

Result = es.search (index, Search)
 Print (Result)

 

Guess you like

Origin www.cnblogs.com/tudou1223/p/11504663.html