EleasticSearch(四)进阶查询

简介: EleasticSearch(四)进阶查询

多字段或语句查询

GET /book/_validate/query?explain
{
  "query": {
    "multi_match": {
      "query": "童话故事大全",
      "fields": ["name", "description"]
    }
  }
}
  • 解释模式
GET /book/_validate/query?explain
{
  "query": {
    "multi_match": {
      "query": "童话故事大全",
      "fields": ["name", "description"]
    }
  }
}
  • match
GET /book/_validate/query?explain
{
  "query": {
    "multi_match": {
      "query": "童话故事大全",
      "fields": ["name", "description"]
    }
  }
}
  • 对分词or或者and查询
GET /book/_search
{
  "query": {
    "match": {
      "name": {
        "query": "童话故事大全",
        "operator": "and"
      }
    }
  }
}
  • term
GET /book/_search
{
  "query": {
    "term": {
      "name": {
        "value": "中华故事"
      }
    }
  }
}

不会对name进行分词,直接使用中华故事进行作关键字查询

GET /book/_search
{
  "query": {
    "terms": {
      "name": [
        "中华",
        "故事"
      ]
    }
  }
}
  • 中华故事其中之一即可

  • 最小匹配查询
#分析一下看下分了几个词
GET /book/_analyze
{
  "field": "name",
  "text": "故事大全"
}
#最小匹配查询
GET /book/_search
{
  "query": {
    "match": {
      "name": {
        "query": "故事大全",
        #最少匹配两个词
        "minimum_should_match": 2
      }
    }
  }
}
  • 多字段添加权重
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "花朵",
      "fields": ["name^10","description"]
    }
  }
}
  • 权重平滑处理,更加突出权重,最大值加上其他值的0.3倍
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "花朵",
      "fields": ["name^10","description"],
      "tie_breaker": 0.3
    }
  }
}
  • 取最好的字段
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["name","description"],
      "type": "best_fields"
    }
  }
}
  • 多字段分值相加
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "大自然的旅行故事",
      "fields": ["name","description"],
      "type": "most_fields"
    }
  }
}
  • query_string
GET /book/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "大自然 AND 旅行"
    }
  }
}

可使用 AND OR NOT

  • bool查询
  • should查询,其中有一个条件为true即可,但true越多的排在越前面
GET /book/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "安徒生"
          }
        },
        {
          "match": {
            "description": "丑小鸭"
          }
        }
      ]
    }
  }
}
  • must查询,必须全部为true
GET /book/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "安徒生"
          }
        },
        {
          "match": {
            "description": "丑小鸭"
          }
        }
      ]
    }
  }
}
  • must_not,必须全部为false
GET /book/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "安徒生"
          }
        },
        {
          "match": {
            "description": "丑小鸭"
          }
        }
      ]
    }
  }
}
  • filter
GET /book/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": 2000,
              "gte": 1
            }
          }
        },
        {
          "match": {
            "name":"故事"
          }
        }
      ]
    }
  } ,
  "sort": [
    {
      "commentNum": {
        "order": "desc"
      }
    }
  ]
}
  • 同义词查询
  • 在每个es的config/analysis-ik新建文件夹synonyms.txt
  • 编辑以下内容
苹果,iphone,apple
美丽,漂亮,气质好
  • 建立索引
PUT /tests
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym",
          "synonyms_path": "analysis-ik/synonyms.txt"
        }
      },
      "analyzer": {
        "ik_syno": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "my_synonym_filter"
          ]
        },
        "ik_syno_max": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [
            "my_synonym_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "ik_syno_max",
          "search_analyzer": "ik_syno"
        }
      }
    }
  }
}
  • 插入两条数据
POST /tests/_doc/1/_create
{
  "name":"苹果"
}
POST /tests/_doc/2/_create
{
  "name":"apple"
}
  • 尝试同义词查询
GET /tests/_search
{
  "query": {
    "match": {
      "name": "iphone"
    }
  }
}
  • 查询结果
{
  "took" : 38,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.33425623,
    "hits" : [
      {
        "_index" : "tests",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.33425623,
        "_source" : {
          "name" : "苹果"
        }
      },
      {
        "_index" : "tests",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.33425623,
        "_source" : {
          "name" : "apple"
        }
      }
    ]
  }
}
目录
相关文章
|
10月前
|
监控 数据库 索引
面试题21:如何优化查询命令?
面试题21:如何优化查询命令?
|
JSON 安全 搜索推荐
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(一)
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(一)
1157 1
|
存储 SQL 缓存
|
存储 SQL 缓存
四.全文检索ElasticSearch经典入门-字符串查询&批量查询&DSL查询过滤&乐观锁
四.全文检索ElasticSearch经典入门-字符串查询&批量查询&DSL查询过滤&乐观锁
|
SQL JSON NoSQL
文档的更多查询 | 学习笔记
快速学习 文档的更多查询
125 0
文档的更多查询 | 学习笔记
|
JSON 分布式计算 Spark
查询所有|学习笔记
快速学习查询所有。
查询所有|学习笔记
|
开发者 索引 Python
索引进阶|学习笔记
快速学习索引进阶
121 0
索引进阶|学习笔记
|
开发者 索引
查询|学习笔记
快速学习查询。
|
分布式计算 API Spark

相关课程

更多