I have got a index that it saved articles. The articles index has got abstract and content field. I have set text
type for abstract and content mapping.
I wanna search two words what appear in the same sentence in abstract or content field. And also I must check the slop words between theirs.
GET articles/_mapping
Output:
{
"articles": {
"mappings": {
"properties": {
"abstract": {
"type": "text",
},
"content {
"type": "text",
}
}
}
}
}
I know that I can search in search in each fields by using span_near
, but how I can use span_near
to search tow words be appeared in the same sentence?
Here is the syntax for using the span_near
GET /articles/_search
{
"query": {
"bool": {
"should": [
{
"span_near": {
"clauses": [
{
"span_term": {
"abstract": "word1"
}
},
{
"span_term": {
"abstract": "word2"
}
}
],
"slop": 0,
"in_order": true
}
},
{
"span_near": {
"clauses": [
{
"span_term": {
"content": "word1"
}
},
{
"span_term": {
"content": "word2"
}
}
],
"slop": 0,
"in_order": true
}
}
]
}
}
}
Note : Adjust the slop values as needed
-
slop
: This defines the maximum number of intervening unmatched positions (words) allowed between the matched terms. Aslop
of0
means the words must appear directly next to each other. -
in_order
– When set totrue
, it requires the words to appear in the specified order (i.e.,word1
must appear beforeword2
). If you don’t care about the order, set it tofalse
.
Refer : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
1