Elasticsearch习题 2.分词器、DSL

Elasticsearch习题 2.分词器、DSL

Task 1

索引task1,包含字段titlefield_a,写出DSL ,要求:

  • 要求 title字段中包含xxx或者sss

  • 结果先按照field_a字段正序,再按照评分倒序

  • title中高亮匹配结果,并且用b标签嵌套

答案:


PUT task/_bulk
{"index": {"_index": "task1", "_id": 1}}
{"title": "xxx 11111", "fiekld_a": 456}
{"index": {"_index": "task1", "_id": 2}}
{"title": "22222 sss", "fiekld_a": 123}
{"index": {"_index": "task1", "_id": 3}}
{"title": "3333 xxx", "fiekld_a": 789}
{"index": {"_index": "task1", "_id": 4}}
{"title": "zz aaaa", "fiekld_a": 11}


GET task1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "xxx"
          }
        },
        {
          "match": {
            "title": "sss"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "fiekld_a": {
        "order": "asc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "title":{
        "pre_tags": ["<b>"],
        "post_tags": ["</b>"]
      }
    }
  }
}

Task 2

有如下索引

POST inject/_doc/1
{
  "title": "the number is error"
}
POST inject/_doc/2
{
  "title": "it is number is error"
}

用下面的查询是返回一条数据

GET inject/_search
{
  "query": {
    "match": {
      "title": "the"
    }
  }
}

要求:

  • 新建一个索引 named inject_new

  • 通过Reindex APIinject的数据和类型复制到inject_new

  • 通过 term查询,term 'the' value of title,返回0条数据。

提示

  • 使用_reindex API可以重建索引

答案:


PUT inject_new
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text", 
        "analyzer": "stop",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "inject"
  },
  "dest": {
    "index": "inject_new"
  }
}

GET inject_new/_search
{
  "query": {
    "term": {
      "title": "number"
    }
  }
}

Task3

索引中index_a有两条分别是wayneswayne's的数据,要求:

  • index_a通过reindexindex_b

  • 对于index_b,使用waynes或wayne's的查询,能返回同样数量的文档和评分

答案:


POST index_a/_bulk
{"index": {"_id": "1"}}
{"title": "xxxx waynes ew12"}
{"index": {"_id": "2"}}
{"title": "it is wayne's"}

GET index_a/_mapping

PUT index_b
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter":{
          "type": "mapping",
          "mappings": [
            "wayne's => waynes"
          ]
        }
      }, 
      "analyzer": {
        "my_analyer": {
          "type": "custom",
          "tokenizer": "standard",
          "char_filter": ["my_char_filter"],
          "filter": []
        }
      }
    }
  }, 
  "mappings" : {
    "properties" : {
      "title" : {
        "type" : "text",
        "analyzer": "my_analyer", 
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "index_a"
  },
  "dest": {
    "index": "index_b"
  }
}


GET index_b/_search
{
  "query": {
    "match": {
      "title": "waynes"
    }
  }
}

GET index_b/_search
{
  "query": {
    "match": {
      "title": "wayne's"
    }
  }
}

本文由 在码圈 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站部分内容收集于互联网,如果有侵权内容、不妥之处,请联系我们删除。敬请谅解!
原文链接:https://www.bedebug.com/archives/elasticsearch-exe2
最后更新于:2022-03-01 17:34:38

请博主喝咖啡 ☕.