美文网首页
ES常用代码

ES常用代码

作者: AC编程 | 来源:发表于2023-04-10 10:11 被阅读0次

    一、查看索引字段类型

    GET order_doc/_mapping
    

    二、ik_max_word

    GET _analyze
    {
      "analyzer": "ik_max_word",
      "text": "小米"
    }
    

    三、ik_smart

    GET _analyze
    {
      "analyzer": "ik_smart",
      "text": "好的吧"
    }
    

    四、SQL

    GET /_sql?format=txt
    {
        "query":"select school,schoolFullName from qmschool"
    }
    

    五、查询所有

    GET /qmschool/_search
    {
      "query": {
        "match_all": {}
      }
    }
    

    六、查询带条件

    GET /qmschool/_search
    {
      "query": {
        "match": {
          "schoolFullName": {
            "query": "中山大学",
            "operator": "AND",
            "prefix_length": 0,
            "max_expansions": 50,
            "fuzzy_transpositions": true,
            "lenient": false,
            "zero_terms_query": "NONE",
            "auto_generate_synonyms_phrase_query": true,
            "boost": 1.0
          }
        }
      }
    }
    

    七、查询条件嵌套

    select * from where cityCode = '430100000000' and (cityName = '还读书文化' or title='还读书文化')

    GET /qm_goods/_search
    {
        "from": 0,
        "size": 10,
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "cityCode": {
                                "value": "430100000000",
                                "boost": 1.0
                            }
                        }
                    },
                    {
                        "bool": {
                            "should": [
                                {
                                    "multi_match": {
                                        "query": "还读书文化",
                                        "fields": [
                                            "cityName^1.0",
                                            "topicStr^1.0"
                                        ],
                                        "type": "best_fields",
                                        "operator": "AND",
                                        "slop": 0,
                                        "prefix_length": 0,
                                        "max_expansions": 50,
                                        "zero_terms_query": "NONE",
                                        "auto_generate_synonyms_phrase_query": true,
                                        "fuzzy_transpositions": true,
                                        "boost": 1.0
                                    }
                                },
                                {
                                    "multi_match": {
                                        "query": "还读书文化",
                                        "fields": [
                                            "address^1.0",
                                            "buTypeStr^1.0",
                                            "content^1.0",
                                            "title^1.0"
                                        ],
                                        "type": "best_fields",
                                        "operator": "OR",
                                        "slop": 0,
                                        "prefix_length": 0,
                                        "max_expansions": 50,
                                        "zero_terms_query": "NONE",
                                        "auto_generate_synonyms_phrase_query": true,
                                        "fuzzy_transpositions": true,
                                        "boost": 1.0
                                    }
                                }
                            ]
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1.0
            }
        }
    }
    

    java代码

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    
    if (StringUtils.isNotBlank(fieldterm)) {
      if (StringUtils.isBlank(termValue)) {
          throw new QiMiaoException("termValue值不能为空");
      }
      TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(fieldterm, termValue);
      boolQueryBuilder.must(termQueryBuilder);
    }
    
    if (StringUtils.isNotBlank(keyword)) {
      BoolQueryBuilder boolShouldQueryBuilder = QueryBuilders.boolQuery();
      //精确查询(关键字不分词查询)
      if (CollectionUtil.isNotEmpty(fieldUnSplitList)) {
          MultiMatchQueryBuilder unSplitMultiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, ArrayUtil.toArray(fieldUnSplitList, String.class));
          unSplitMultiMatchQueryBuilder.operator(Operator.AND);
          boolShouldQueryBuilder.should(unSplitMultiMatchQueryBuilder);
      }
    
      //分词查询
      if (CollectionUtil.isNotEmpty(fieldList)) {
          MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, ArrayUtil.toArray(fieldList, String.class));
          multiMatchQueryBuilder.operator(Operator.OR);
          boolShouldQueryBuilder.should(multiMatchQueryBuilder);
      }
    
      boolQueryBuilder.must(boolShouldQueryBuilder);
    }
    builder.query(boolQueryBuilder);
    

    相关文章

      网友评论

          本文标题:ES常用代码

          本文链接:https://www.haomeiwen.com/subject/wkgiddtx.html