Python-elasticsearch Search __python

Source: Internet
Author: User

Elasticsearch is an open source search engine, built on the basis of a Full-text search engine library Apache lucene™. Lucene may be present, whether open source or private, with the most advanced, high-performance and full-featured search engine features of the library. But Lucene is just a library. To take advantage of it, you need to write Java programs and integrate the Lucene packages directly into your Java program. Worse, you need to have a certain degree of understanding of information retrieval to understand how Lucene works. Lucene is very complex.
In the previous blog to introduce the simple use of elasticsearch, then record the Elasticsearch query: query all the data

# Search All data
es.search (index= "My_index", doc_type= "Test_type")

# or body
= {"
    query": {"
        Match_all": {}
    }
}
es.search (index= "My_index", doc_type= "Test_type", Body=body)
term and Terms
# term Body
= {"Query": {"
        term": {
            "name": "Python"}}}
# query all data name= "Python"
Es.search (index= "My_index", doc_type= "Test_type", Body=body)


# terms Body
= {
    "query": {
        " Terms ": {"
            name ": [
                " Python "," Android "
            ]}}
# Search out name=" python "or name=" Android "All data
Es.search (index=" My_index ", doc_type=" Test_type ", Body=body)
Match and Multi_match
# match: Match the name contains the Python keyword data body
= {
    "query": {
        "match": {
            "name": "Python"
        }
    }
# Query name contains data es.search for Python keywords
(index= "My_index", doc_type= "Test_type", Body=body)

# Multi_match : Matching the data in name and addr containing the Shenzhen keyword
= {"
    query": {"
        Multi_match": {"
            query": "Shenzhen",
            "Fields": [" Name "," Addr "}}}
# query name and addr data es.search containing the" Shenzhen "keyword
(index=" My_index ", doc_type=" Test_type ", Body=body)
IDs
BODY = {"Query": {"IDs": {"
            Type": "Test_type",
            "values": [
                "1", "2"
            ]
}}}
# Search for all data es.search with ID 1 or 2d
(index= "My_index", doc_type= "Test_type", Body=body)
Compound query bool

BOOL has 3 kinds of query relationships, must (all satisfied), should (one of them satisfied), Must_not (none satisfied)

BODY = {"Query": {"bool": {"
            must": [
                {"
                    term": {"
                        name": "Python"
                    }
                },
                {
                    "term": {"Age":}}
                    ]}
# get Name= "python" and age= 18 of all data
Es.search (index= "My_index", doc_type= "Test_type", Body=body)
Slicing Query
BODY = {"
    query": {
        "Match_all": {}
    }
    ' from ': 2    # starting with second data
    ' size ': 4    # get 4 data
}
# Starting with the 2nd data, get 4 data
Es.search (index= "My_index", doc_type= "Test_type", Body=body)
Scope Query
BODY = {' query ': {' range ': {' age
            ': {
                ' GTE ':       # >=18
                ' LTE ':        # <=30
            }
        }
    }
}
# query 18<=age<=30 all data
es.search (index= "My_index", doc_type= "Test_type", Body=body)
prefix query
BODY = {"
    query": {"prefix": {"
            name": "P"}}
}
# query all data es.search prefixed with "Zhao"
( index= "My_index", doc_type= "Test_type", Body=body)
Wildcard Query
BODY = {"Query": {"wildcard": {"
            name": "*id"}}}
# query name All data es.search with an ID suffix
( index= "My_index", doc_type= "Test_type", Body=body)
Sort
BODY = {' query ': {'
        Match_all ': {}
    }
    ' sort ': {
        ' age ': {                 # sort ' order ' according to Che of age character
            ' order ': ASC '       # ASC Ascending, desc descending
        }
    }
}
Filter_path

Response filtering

# only need to get _id data, multiple conditions separated by commas
Es.search (index= "My_index", doc_type= "Test_type", filter_path=["hits.hits._id"])

# Get all data
Es.search (index= "My_index", doc_type= "Test_type", filter_path=["hits.hits._*"])
Count

Executes the query and gets the number of matches for the query

# Get Data Volume
Es.count (index= "My_index", doc_type= "Test_type")
Metric Class AggregationGet Minimum value
BODY = {"
    query": {
        "Match_all": {}
    },
    "Aggs": {                        # Aggregate query
        "Min_age": {                 # Min of the minimum value
            : {                 # Minimum
                ' field ': ' Age '       # query ' age ' Minimum}}
            }
# Search all data and get the minimum age value
es.search ( index= "My_index", doc_type= "Test_type", Body=body)
Get maximum Value
BODY = {
    "query": {
        "Match_all": {}
    },
    "Aggs": {                        # Aggregate query
        "Max_age": {                 # max value key
            ' Max ': {                 # max '
                field ': ' Age '       # query ' age ' for maximum}}}
# Search all data and get the largest value of age
Es.search (index= "My_index", doc_type= "Test_type", Body=body)
Get and
BODY = {"
    query": {
        "Match_all": {}
    },
    "Aggs": {                        # Aggregate query
        "Sum_age": {                 # and key
            ' sum ': {                 # and
                ' field ': ' Age '       # get all Age's and
            }
        }
# to search all data and get all age and
Es.search (index= "My_index", doc_type= "Test_type", Body=body)
Get average
BODY = {
    "query": {
        "Match_all": {}
    },
    "Aggs": {                        # Aggregate query
        "Avg_age": {                 # key for average #
            "sum": {                 # "
                Field": "Age"       # Get the average of all Age
            }}}
# Search all data, Gets the average value of all age
Es.search (index= "My_index", doc_type= "Test_type", Body=body)

More Search Usage:
Https://elasticsearch-py.readthedocs.io/en/master/api.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.