[elasticsearch笔记] Query DSL-intervals

目录

    • intervals 满足不同部分Query
    • match/multi_match/should/match_bool_prefix
    • common/term
    • query_string/simple_query_string

intervals 满足不同部分Query

POST _search
{
  "query": {
    "intervals": {
      "my_text": {
        "all_of": {
          "ordered": true,
          "intervals": [
            {
              "match": {
                "query": "my favourite food",
                "max_gaps": 0,
                "ordered": true
              }
            },
            {
              "any_of": {
                "intervals": [
                  {
                    "match": {
                      "query": "hot water"
                    }
                  },
                  {
                    "match": {
                      "query": "cold porridge"
                    }
                  }
                ]
              }
            }
          ]
        },
        "_name": "favourite_food"
      }
    }
  }
}

POST _search
{
  "query": {
    "intervals" : {
      "my_text" : {
        "match" : {
          "query" : "hot porridge",
          "max_gaps" : 10,
          "filter" : {
            "not_containing" : {
              "match" : {
                "query" : "salty"
              }
            }
          }
        }
      }
    }
  }
}

POST _search
{
  "query": {
    "intervals" : {
      "my_text" : {
        "match" : {
          "query" : "hot porridge",
          "filter" : {
            "script" : {
              "source" : "interval.start > 10 && interval.end < 20 && interval.gaps == 0"
            }
          }
        }
      }
    }
  }
}

POST _search
{
  "query": {
    "intervals" : {
      "my_text" : {
        "match" : {
          "query" : "salty",
          "filter" : {
            "contained_by" : {
              "match" : {
                "query" : "hot porridge"
              }
            }
          }
        }
      }
    }
  }
}

POST _search
{
  "query": {
    "intervals": {
      "my_text": {
        "all_of": {
          "intervals": [
            {
              "match": {
                "query": "the"
              }
            },
            {
              "any_of": {
                "intervals": [
                  {
                    "match": {
                      "query": "big"
                    }
                  },
                  {
                    "match": {
                      "query": "big bad"
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "query": "wolf"
              }
            }
          ],
          "max_gaps": 0,
          "ordered": true
        }
      }
    }
  }
}

POST _search
{
  "query": {
    "intervals": {
      "my_text": {
        "any_of": {
          "intervals": [
            {
              "match": {
                "query": "the big bad wolf",
                "ordered": true,
                "max_gaps": 0
              }
            },
            {
              "match": {
                "query": "the big wolf",
                "ordered": true,
                "max_gaps": 0
              }
            }
          ]
        }
      }
    }
  }
}

match/multi_match/should/match_bool_prefix


GET /_search
{
  "query": {
    "match": {
      "message": {
        "query": "this is a test",
        "operator": "and",
        "zero_terms_query": "all"
      }
    }
  }
}

GET /_search
{
  "query": {
    "match": {
      "message": {
        "query": "to be or not to be",
        "cutoff_frequency": 0.001
      }
    }
  }
}

# (ny OR (new AND york)) city
GET /_search
{
  "query": {
    "match": {
      "message": {
        "query": "ny city",
        "auto_generate_synonyms_phrase_query": false
      }
    }
  }
}

GET /_search
{
  "query": {
    "match_bool_prefix": {
      "message": "quick brown f"
    }
  }
}

GET /_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "message": "quick"
          }
        },
        {
          "term": {
            "message": "brown"
          }
        },
        {
          "prefix": {
            "message": "f"
          }
        }
      ]
    }
  }
}

GET /_search
{
  "query": {
    "match_bool_prefix": {
      "message": {
        "query": "quick brown f",
        "analyzer": "keyword"
      }
    }
  }
}

GET /_search
{
  "query": {
    "match_phrase": {
      "message": "this is a test"
    }
  }
}

GET /_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "this is a test",
        "analyzer": "my_analyzer"
      }
    }
  }
}

GET /_search
{
  "query": {
    "match_phrase_prefix": {
      "message": "quick brown f"
    }
  }
}

GET /_search
{
  "query": {
    "match_phrase_prefix": {
      "message": {
        "query": "quick brown f",
        "max_expansions": 10
      }
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "this is a test",
      "fields": [
        "subject",
        "message"
      ]
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "Will Smith",
      "fields": [
        "title",
        "*_name"
      ]
    }
  }
}

# The subject field is three times as important as the message field.
GET /_search
{
  "query": {
    "multi_match": {
      "query": "this is a test",
      "fields": [
        "subject^3",
        "message"
      ]
    }
  }
}

# score= the score from the best matching field + tie_breaker * _score for all other matching fields
GET /_search
{
  "query": {
    "multi_match": {
      "query": "brown fox",
      "type": "best_fields",
      "fields": [
        "subject",
        "message"
      ],
      "tie_breaker": 0.3
    }
  }
}

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "subject": "brown fox"
          }
        },
        {
          "match": {
            "message": "brown fox"
          }
        }
      ],
      "tie_breaker": 0.3
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "Will Smith",
      "type": "best_fields",
      "fields": [
        "first_name",
        "last_name"
      ],
      "operator": "and"
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "quick brown fox",
      "type": "most_fields",
      "fields": [
        "title",
        "title.original",
        "title.shingles"
      ]
    }
  }
}

# same to above
GET /_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "quick brown fox"
          }
        },
        {
          "match": {
            "title.original": "quick brown fox"
          }
        },
        {
          "match": {
            "title.shingles": "quick brown fox"
          }
        }
      ]
    }
  }
}

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "quick brown f",
      "type":       "phrase_prefix",
      "fields":     [ "subject", "message" ]
    }
  }
}

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match_phrase_prefix": {
            "subject": "quick brown f"
          }
        },
        {
          "match_phrase_prefix": {
            "message": "quick brown f"
          }
        }
      ]
    }
  }
}

# +(first_name:will  last_name:will)
# +(first_name:smith last_name:smith)
GET /_search
{
  "query": {
    "multi_match": {
      "query": "Will Smith",
      "type": "cross_fields",
      "fields": [
        "first_name",
        "last_name"
      ],
      "operator": "and"
    }
  }
}

# edge_ngram
GET /_search
{
  "query": {
    "multi_match": {
      "query": "Jon",
      "type": "cross_fields",
      "fields": [
        "first",
        "first.edge",
        "last",
        "last.edge"
      ]
    }
  }
}

GET /_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "Will Smith",
            "type": "cross_fields",
            "fields": [
              "first",
              "last"
            ],
            "minimum_should_match": "50%"
          }
        },
        {
          "multi_match": {
            "query": "Will Smith",
            "type": "cross_fields",
            "fields": [
              "*.edge"
            ]
          }
        }
      ]
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "Jon",
      "type": "cross_fields",
      "analyzer": "standard",
      "fields": [
        "first",
        "last",
        "*.edge"
      ]
    }
  }
}

GET /_search
{
  "query": {
    "multi_match": {
      "query": "quick brown f",
      "type": "bool_prefix",
      "fields": [
        "subject",
        "message"
      ]
    }
  }
}

common/term

  • 类似 term,但是分高频词(不重要)及低频词(重要)
GET /_search
{
  "query": {
    "common": {
      "body": {
        "query": "this is bonsai cool",
        "cutoff_frequency": 0.001
      }
    }
  }
}

GET /_search
{
  "query": {
    "common": {
      "body": {
        "query": "nelly the elephant as a cartoon",
        "cutoff_frequency": 0.001,
        "low_freq_operator": "and"
      }
    }
  }
}
# roughly equivalent to above
GET /_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "body": "nelly"
          }
        },
        {
          "term": {
            "body": "elephant"
          }
        },
        {
          "term": {
            "body": "cartoon"
          }
        }
      ],
      "should": [
        {
          "term": {
            "body": "the"
          }
        },
        {
          "term": {
            "body": "as"
          }
        },
        {
          "term": {
            "body": "a"
          }
        }
      ]
    }
  }
}

GET /_search
{
  "query": {
    "common": {
      "body": {
        "query": "nelly the elephant as a cartoon",
        "cutoff_frequency": 0.001,
        "minimum_should_match": 2
      }
    }
  }
}

GET /_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "should": [
            {
              "term": {
                "body": "nelly"
              }
            },
            {
              "term": {
                "body": "elephant"
              }
            },
            {
              "term": {
                "body": "cartoon"
              }
            }
          ],
          "minimum_should_match": 2
        }
      },
      "should": [
        {
          "term": {
            "body": "the"
          }
        },
        {
          "term": {
            "body": "as"
          }
        },
        {
          "term": {
            "body": "a"
          }
        }
      ]
    }
  }
}

GET /_search
{
  "query": {
    "common": {
      "body": {
        "query": "nelly the elephant not as a cartoon",
        "cutoff_frequency": 0.001,
        "minimum_should_match": {
          "low_freq": 2,
          "high_freq": 3
        }
      }
    }
  }
}
# roughly equivalent to above
GET /_search
{
  "query": {
    "bool": {
      "must": {
        "bool": {
          "should": [
            {
              "term": {
                "body": "nelly"
              }
            },
            {
              "term": {
                "body": "elephant"
              }
            },
            {
              "term": {
                "body": "cartoon"
              }
            }
          ],
          "minimum_should_match": 2
        }
      },
      "should": {
        "bool": {
          "should": [
            {
              "term": {
                "body": "the"
              }
            },
            {
              "term": {
                "body": "not"
              }
            },
            {
              "term": {
                "body": "as"
              }
            },
            {
              "term": {
                "body": "a"
              }
            }
          ],
          "minimum_should_match": 3
        }
      }
    }
  }
}

query_string/simple_query_string

GET /_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "this AND that OR thus"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "(new york city) OR (big apple)"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "content",
        "name"
      ],
      "query": "this AND that"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "query": "(content:this OR name:this) AND (content:that OR name:that)"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "content",
        "name^5"
      ],
      "query": "this AND that OR thus",
      "tie_breaker": 0
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "city.*"
      ],
      "query": "this AND that OR thus"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "query": "city.\\*:(this AND that OR thus)"
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "content",
        "name.*^5"
      ],
      "query": "this AND that OR thus"
    }
  }
}

# (ny OR (new AND york)) city
GET /_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "ny city",
      "auto_generate_synonyms_phrase_query": false
    }
  }
}

# (title:this title:that title:thus)~2
GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "title"
      ],
      "query": "this that thus",
      "minimum_should_match": 2
    }
  }
}

# ((content:this content:that content:thus) | (title:this title:that title:thus))
GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "title",
        "content"
      ],
      "query": "this that thus",
      "minimum_should_match": 2
    }
  }
}

# ((content:this | title:this) (content:that | title:that) (content:thus | title:thus))~2
GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "title",
        "content"
      ],
      "query": "this OR that OR thus",
      "minimum_should_match": 2
    }
  }
}

GET /_search
{
  "query": {
    "query_string": {
      "fields": [
        "title",
        "content"
      ],
      "query": "this OR that OR thus",
      "type": "cross_fields",
      "minimum_should_match": 2
    }
  }
}

# using ? to replace a single character, and * to replace zero or more characters
# Leading wildcards can be disabled by setting allow_leading_wildcard to false.

GET /_search
{
  "query": {
    "simple_query_string": {
      "query": """"fried eggs" +(eggplant | potato) -frittata""",
      "fields": [
        "title^5",
        "body"
      ],
      "default_operator": "and"
    }
  }
}

GET /_search
{
  "query": {
    "simple_query_string": {
      "fields": [
        "content"
      ],
      "query": "foo bar -baz"
    }
  }
}

GET /_search
{
  "query": {
    "simple_query_string": {
      "fields": [
        "content",
        "name.*^5"
      ],
      "query": "foo bar baz"
    }
  }
}

GET /_search
{
  "query": {
    "simple_query_string": {
      "query": "foo | bar + baz*",
      "flags": "OR|AND|PREFIX"
    }
  }
}

# (ny OR (new AND york)) city)
GET /_search
{
  "query": {
    "simple_query_string": {
      "query": "ny city",
      "auto_generate_synonyms_phrase_query": false
    }
  }
}

你可能感兴趣的:(elasticsearch)