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

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

elastic stack - elasticsearch must_not returning wrong values

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"
        }
      }
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...