Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to figure out what I'm doing wrong with my Elastic query. I'm trying to filter out all the docs that have "Software Engineer" as their title. my Query:

{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "must": [{
        "bool": {
          "must_not": [{
            "term": {
              "title.keyword": "Software Engineer"
            }
          }]
        }
      }]
    }
  }
}

in my mapping...

"title": {"type": "text"}

Then my results:

hits:[
{title: "Software Engineer"},
{title: "Engineer"},
{title: "Software Engineer"},
{title: "Software and Data Quality Manager"},
...
]

I would like to not get Software Engineer in my search results here. Any help would be greatly appreciated!

question from:https://stackoverflow.com/questions/66064654/elasticsearch-must-not-returning-wrong-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use multi-fields if you want to map the title field as text and keyword type. Modify your index mapping to

{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "keyword": { 
            "type":  "keyword"
          }
        }
      }
    }
  }
}

Index Data:

{
  "title": "Software and Data Quality Manager"
}
{
  "title": "Software Engineer"
}
{
  "title": "Engineer"
}

Search Query:

{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "title.keyword": "Software Engineer"
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "66064654",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.0,
        "_source": {
          "title": "Engineer"
        }
      },
      {
        "_index": "66064654",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.0,
        "_source": {
          "title": "Software and Data Quality Manager"
        }
      }
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...