天天看點

Elaticsearch第十篇 Es的文檔的DSL搜尋

1、建立索引shop

2、 建立映射 127.0.0.1:9200/shop/_mapping

{
    "properties": {
        "id": {
            "type": "long"
        },
        "age": {
            "type": "integer"
        },
        "username": {
            "type": "keyword"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "money": {
            "type": "float"
        },
        "desc": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "sex": {
            "type": "byte"
        },
        "birthday": {
            "type": "date"
        },
        "face": {
            "type": "text",
            "index": false
        }
    }
}
           

3、中文分詞

4、添加資料

5、DSL搜尋

a查詢與多條件查詢

/demeter_index/_search?q=nickname:馬克波羅
           
/demeter_index/_search?q=nickname:馬克波羅&q=age:20
           

b keyword與 text的差別

/demeter_index/_search?q=usename:super hero
           

結構化搜尋 =》DSL搜尋(domain specific language)

/demeter_index/_doc/_search

{
"query":{
    "match":{
        "desc":"馬可波羅"
    }
}

}
           
{
    "query": {
        "match": {
            "username": "super hero"
        }
    }
}
           

等價于 /demeter_index/_search?q=username:super hero 要完全比對,才能搜尋的出來。

d 關鍵字exists

{
    "query": {
        "exists": {
            "field": "username"
        }
    }
}
           

e 查詢所有

get方法 

/demeter_index/_doc/_search
           

 等價于 post方法  /demeter_index/_doc/_search

{
    "query": {
        "match_all": {}
    }
}
           

f 傳回固定字段

/demeter_index/_doc/_search  post方法

{
    "query": {
        "match_all": {}
    },
    "_source": [
        "id",
        "nickname",
        "age"
    ]
}
           

---------------------

然後加一條資料

{
  "id": 1020,
  "age": 31,
  "username": "馬克",
  "nickname": "大鳳梨",
  "money": 188.8,
  "desc": "馬可大學畢業了, 然後去國外留學",
  "sex": 1,
  "birthday": "1980-08-14",
  "face": "https://www.codedemeter.com/static/img/index/logo.png"
}
           

發現:

{
    "query": {
        "term": {
            "desc": "馬可波羅"
        }
    }
}
           

是兩條資料。類似圖形化界面這樣的兩條

Elaticsearch第十篇 Es的文檔的DSL搜尋

match就會有 三條。

多了一條,馬可上大學,去國外了。

總結:term是代表完全比對,也就是精确查詢,搜尋前不會再對搜尋詞進行分詞,是以搜尋必須是文檔分詞集合中的一個

match查詢會對搜尋詞進行分詞,隻要搜尋詞的分詞集合中的一個或多個存在與文檔中就會被查詢到

term相當于 精确比對。

 match相當于text,分詞之後,再去比對。分詞完畢之後再去搜尋。 如下,就是會有馬可的,波羅的,馬可波羅的

{
    "query": {
        "match": {
            "desc": "馬可波羅"
        }
    }
}
           

terms搜尋

{
    "query": {
        "terms": {
            "desc": ["馬可","波羅"]
        }
    }
}
           

參文章:https://blog.csdn.net/u012748043/article/details/106408366

繼續閱讀