Elasticsearch全文搜索的解决方案,走了不少弯路

本文探讨了在使用Elasticsearch进行全文搜索时遇到的问题,重点在于正确配置mapping中的_all字段,以提高搜索效率并避免资源浪费。通过合理设置_all字段及调整其他属性,可以显著提升搜索体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在对Elasticsearch进行全文搜索时,走了不少弯路。其实最终发现就一点点设置要的问题。

1.注意在mapping里面将_all enabled

示例:

{
    "template": "dbagent_*",
    "settings": {
        "index.number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "_default_": {
            "_all": {
                "enabled": "true"
            },
            "_source": {
                "enabled": true,
                "compress": true
            },
            "properties": {
                "_id": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "timestamp": {
                    "enabled": true,
                    "type": "date",
                    "format": "dateOptionalTime"
                },
                "host": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        },
	"access":{
		"properties":{
	          "status":{
			  "type":"integer"
		  },
                  "chost": {
                          "type": "ip",
                          "index": "not_analyzed"
                   },
 		  "interface":{
			   "dynamic":true,			
			   "type":"string",
			   "index":"not_analyzed"
		    }
		}
	},
        "error":{
 		"properties":{
			"pid":{
				"type":"integer"
			},
			"level":{
				"type":"integer"
			},
			"msg":{
				"type":"string",
				"index":"not_analyzed"
			},
			"ctime":{
				"dynamic":true,
				"type":"long"
			},
			"interface":{
				"dynamic":true,			
				"type":"string",
				"index":"not_analyzed"
			},
			"para":{
				"dynamic":true,				
				"type":"string",
				"index":"not_analyzed"
			},
               		"chost": {
				"dynamic":true, 	         	    	
                  	    	"type": "ip",
                  	    	"index": "not_analyzed"
                        },
			"message":{
				"type":"string",
				"index":"not_analyzed"
			}
		}
	}
    }
}


搜索时:

{"query":{"bool":{"must":[{"query_string":{"default_field":"_all","query":"455"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"facets":{}}

es 对 _all的解释

url:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-all-field.html

原文:

The idea of the _all field is that it includes the text of one or more other fields within the document indexed. It can come very handy especially for search requests, where we want to execute a search query against the content of a document, without knowing which fields to search on. This comes at the expense of CPU cycles and index size.

The _all fields can be completely disabled. Explicit field mappings and object mappings can be excluded / included in the _all field. By default, it is enabled and all fields are included in it for ease of use.

When disabling the _all field, it is a good practice to set index.query.default_field to a different value (for example, if you have a main "message" field in your data, set it to message).

One of the nice features of the _all field is that it takes into account specific fields boost levels. Meaning that if a title field is boosted more than content, the title (part) in the _all field will mean more than the content (part) in the _all field.

Here is a sample mapping:

{
   
"person" : {
       
"_all" : {"enabled" : true},
       
"properties" : {
           
"name" : {
               
"type" : "object",
               
"dynamic" : false,
               
"properties" : {
                   
"first" : {"type" : "string", "store" : true , "include_in_all" : false},
                   
"last" : {"type" : "string", "index" : "not_analyzed"}
               
}
           
},
           
"address" : {
               
"type" : "object",
               
"include_in_all" : false,
               
"properties" : {
                   
"first" : {
                       
"properties" : {
                           
"location" : {"type" : "string", "store" : true, "index_name" : "firstLocation"}
                       
}
                   
},
                   
"last" : {
                       
"properties" : {
                           
"location" : {"type" : "string"}
                       
}
                   
}
               
}
           
},
           
"simple1" : {"type" : "long", "include_in_all" : true},
           
"simple2" : {"type" : "long", "include_in_all" : false}
       
}
   
}
}

The _all fields allows for storeterm_vector and analyzer (with specific index_analyzerand search_analyzer) to be set.

highlightingedit

For any field to allow highlighting it has to be either stored or part of the _source field. By default the _all field does not qualify for either, so highlighting for it does not yield any data.

Although it is possible to store the _all field, it is basically an aggregation of all fields, which means more data will be stored, and highlighting it might produce strange results.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值