Elasticsearch如何使用同义词搜索富文本html标签过滤以及分权限过滤搜索结果

如何建立恰当的索引结点

{
    "mappings": {
        "data": {
        "properties": {
          "answer_id": {
            "type": "long"
          },
          "content": {
            "type": "text",
            "analyzer": "html_analyze"
          },
          "question_id": {
            "type": "long"
          },
          "title": {
            "type": "text",
            "analyzer": "html_analyze"
          },
          "authority":{    //权限数组[1,2,3,4]
              "type": "byte"
          }
        }
      }
    },
    "settings": {
      "analysis": {
          "filter": {    //定义一个同义词过滤器
            "local_synonym": {
              "type": "synonym",
              "synonyms_path": "analysis/synonym.txt"
            }
          },
          "analyzer": {
            "html_analyze": {    //定义一个html标签过滤的分词器
              "filter": [
                "local_synonym"
              ],
              "char_filter": [
                "my_char_filter"
              ],
              "type": "custom",
              "tokenizer": "ik_max_word"
            }
          },
          "char_filter": {
            "my_char_filter": {
              "escaped_tags": [],
              "type": "html_strip"
            }
          }
        }
    }
}
上传到ElasticSearch的数据格式如下:
{
    "answer_id": 1,
    "content": "内容",
    "question_id": 2,
    "title": "标题",
    "authority": [1,2,3,4] //权限有数组控制,每个user有自己对应的权限
}

如何使用高亮并且进行权限过滤搜索

 {
    "from": 0,
    "size": 10,
    "query":{
        "bool": {
            "must":{
                "multi_match": {
                    "query": "测试",
                    "type": "best_fields",
                    "fields": ["title^2", "content"],
                    "tie_breaker": 0.3
                }
            },
            "filter": {
                "term": {
                    "authority": "1" // 过滤出权限为1的结果集
                }
            }
        }
    },
    "highlight": {
        "pre_tags": [""],
        "post_tags": [""],
        "fields": {
            "title": {"number_of_fragments":0},
            "content": {"fragment_size": 100}
        }
    }
}

你可能感兴趣的:(Elasticsearch如何使用同义词搜索富文本html标签过滤以及分权限过滤搜索结果)